Skip to content

Commit

Permalink
Added specialization of parallel_for for size_t.
Browse files Browse the repository at this point in the history
Fixes Issue #19. Also modified array_test.cpp to verify this case.
  • Loading branch information
harrism committed Dec 22, 2015
1 parent afa959a commit 3adb866
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 13 additions & 0 deletions hemi/parallel_for.h
Expand Up @@ -39,4 +39,17 @@ namespace hemi
for (auto idx : grid_stride_range(first, last)) function(idx);
});
}

template <typename F>
void parallel_for(size_t first, size_t last, F function) {
ExecutionPolicy p;
parallel_for(p, first, last, function);
}

template <typename F>
void parallel_for(const ExecutionPolicy &p, size_t first, size_t last, F function) {
hemi::launch(p, [=] HEMI_LAMBDA () {
for (auto idx : grid_stride_range(first, last)) function(idx);
});
}
}
10 changes: 5 additions & 5 deletions test/test_array.cpp
Expand Up @@ -43,10 +43,10 @@ TEST(ArrayTest, CreatesAndFillsArrayOnDevice)
ASSERT_SUCCESS(hemi::deviceSynchronize());
}

template <typename T>
void squareOnDevice(T* ptr, int n) {
hemi::parallel_for(0, n, [=] HEMI_LAMBDA (int i) {
ptr[i] = ptr[i]*ptr[i];
void squareOnDevice(hemi::Array<float> &a) {
float *ad = a.ptr();
hemi::parallel_for(0, a.size(), [=] HEMI_LAMBDA (int i) {
ad[i] = ad[i]*ad[i];
});
}

Expand All @@ -61,7 +61,7 @@ TEST(ArrayTest, FillsOnHostModifiesOnDevice)
float *ptr = data.writeOnlyHostPtr();
std::fill(ptr, ptr+n, val);

squareOnDevice(data.ptr(), n);
squareOnDevice(data);

for(int i = 0; i < n; i++) {
ASSERT_EQ(val*val, data.readOnlyPtr(hemi::host)[i]);
Expand Down

0 comments on commit 3adb866

Please sign in to comment.