Skip to content

Commit

Permalink
add a stdpar test for array of floats and vector<int> (#24)
Browse files Browse the repository at this point in the history
Co-authored-by: leggett <[email protected]>
  • Loading branch information
cgleggett and leggett authored Sep 30, 2024
1 parent b987e4b commit 1659d34
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 6 deletions.
4 changes: 3 additions & 1 deletion FastCaloSimAnalyzer/FastCaloGpu/FastCaloGpu/TestStdPar.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class TestStdPar {

void testAll(unsigned long);

void test_vector(unsigned long);
void test_floatArray(unsigned long);
void test_vecInt(unsigned long);
void test_vecFloat(unsigned long);
void test_atomicAdd_int(unsigned long);
void test_atomicAdd_float(unsigned long);

Expand Down
103 changes: 99 additions & 4 deletions FastCaloSimAnalyzer/FastCaloGpu/src/TestStdPar.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,56 @@
#include <atomic>

void TestStdPar::testAll(unsigned long num) {
test_vector(num);
test_vecInt(num);
test_vecFloat(num);
test_atomicAdd_int(num);
test_atomicAdd_float(num);
}

void TestStdPar::test_vector(unsigned long num) {
void TestStdPar::test_floatArray(unsigned long num) {

std::cout << "---------- test_vec( " << num << " ) -------------\n";
std::cout << "---------- test_floatArray( " << num << " ) -------------\n";

double sum(0.);
float *pf = new float[num];
for (int i=0; i<num; ++i) {
pf[i] = float(i) / 100.;
if (i<10) { printf("float test CPU: %d %f\n", i, pf[i]); }
sum += pf[i] + 1.;
}

std::for_each_n(std::execution::par_unseq, counting_iterator(0), num,
[=](int i) {
if (i<10) { printf(" float test GPU: %d %f\n", i, pf[i]); }
pf[i] += 1.;
} );


double sum2(0.);
for (int i=0; i<num; ++i) {
sum2 += pf[i];
}


if (float(sum) == float(sum2)) {
std::cout << "test OK: " << sum << " == " << sum2 << std::endl;
} else {
std::cout << "test FAIL: " << sum << " != " << sum2 << std::endl;
}

delete pf;

std::cout << "------- done test_floatArray -------------\n";

}



void TestStdPar::test_vecFloat(unsigned long num) {

std::cout << "---------- test_vec<float>( " << num << " ) -------------\n";

double sum(0.);
std::vector<float>* pvec = new std::vector<float>;
pvec->resize(num);

Expand All @@ -25,20 +66,74 @@ void TestStdPar::test_vector(unsigned long num) {
for (int i=0; i<num; ++i) {
pdat[i] = float(i) / 100.;
if (i<10) { printf("vec test CPU: %d %f\n", i, pdat[i]); }
sum += pdat[i] + 1.;
}

std::for_each_n(std::execution::par_unseq, counting_iterator(0), num,
[=](int i) {
if (i<10) { printf("vec test GPU: %d %f\n", i, pdat[i]); }
if (i<10) { printf(" vec test GPU: %d %f\n", i, pdat[i]); }
pdat[i] += 1.;
} );

double sum2(0.);
for (int i=0; i<num; ++i) {
sum2 += pdat[i];
}


if (float(sum) == float(sum2)) {
std::cout << "test OK: " << sum << " == " << sum2 << std::endl;
} else {
std::cout << "test FAIL: " << sum << " != " << sum2 << std::endl;
}

delete pvec;

std::cout << "------- done test_vec(num) -------------\n";

}

void TestStdPar::test_vecInt(unsigned long num) {

std::cout << "---------- test_vec<int>( " << num << " ) -------------\n";

long int sum(0);
std::vector<int>* pvec = new std::vector<int>;
pvec->resize(num);

int* pdat = pvec->data();

for (int i=0; i<num; ++i) {
pdat[i] = i;
if (i<10) { printf("vec test CPU: %d %d\n", i, pdat[i]); }
sum += pdat[i] + 1000;
}

std::for_each_n(std::execution::par_unseq, counting_iterator(0), num,
[=](int i) {
if (i<10) { printf(" vec test GPU: %d %d\n", i, pdat[i]); }
pdat[i] += 1000;
} );

long int sum2(0);
for (int i=0; i<num; ++i) {
sum2 += pdat[i];
}


if (sum == sum2) {
std::cout << "test OK: " << sum << " == " << sum2 << std::endl;
} else {
std::cout << "test FAIL: " << sum << " != " << sum2 << std::endl;
}

delete pvec;

std::cout << "------- done test_vec<int>(num) -------------\n";

}



void TestStdPar::test_atomicAdd_int(unsigned long num) {
std::cout << "---------- test_atomic<int>_add -------------\n";
Expand Down
6 changes: 5 additions & 1 deletion FastCaloSimAnalyzer/Root/TFCSStdParTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ void TFCSStdParTest::test(bool doAtomic, bool doVector, unsigned long num) {
tst.test_atomicAdd_float(num);
}

if (doVector) tst.test_vector(num);
if (doVector) {
tst.test_floatArray(num);
tst.test_vecFloat(num);
tst.test_vecInt(num);
}

}

0 comments on commit 1659d34

Please sign in to comment.