-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new executable runTFCSStdPar to test stdpar features (#23)
- Loading branch information
Showing
9 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef FCS_TEST_STDPAR | ||
#define FCS_TEST_STDPAR 1 | ||
|
||
class TestStdPar { | ||
|
||
public: | ||
|
||
void testAll(unsigned long); | ||
|
||
void test_vector(unsigned long); | ||
void test_atomicAdd_int(unsigned long); | ||
void test_atomicAdd_float(unsigned long); | ||
|
||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include "TestStdPar.h" | ||
#include "CountingIterator.h" | ||
|
||
#include <algorithm> | ||
#include <execution> | ||
#include <iostream> | ||
#include <vector> | ||
#include <atomic> | ||
|
||
void TestStdPar::testAll(unsigned long num) { | ||
test_vector(num); | ||
test_atomicAdd_int(num); | ||
test_atomicAdd_float(num); | ||
} | ||
|
||
void TestStdPar::test_vector(unsigned long num) { | ||
|
||
std::cout << "---------- test_vec( " << num << " ) -------------\n"; | ||
|
||
std::vector<float>* pvec = new std::vector<float>; | ||
pvec->resize(num); | ||
|
||
float* pdat = pvec->data(); | ||
|
||
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]); } | ||
} | ||
|
||
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]); } | ||
pdat[i] += 1.; | ||
} ); | ||
|
||
delete pvec; | ||
|
||
std::cout << "------- done test_vec(num) -------------\n"; | ||
|
||
} | ||
|
||
|
||
void TestStdPar::test_atomicAdd_int(unsigned long num) { | ||
std::cout << "---------- test_atomic<int>_add -------------\n"; | ||
std::atomic<int> *ii = new std::atomic<int>{0}; | ||
std::for_each_n(std::execution::par_unseq, counting_iterator(0), num, | ||
[=](int i) { | ||
int j = (*ii)++; | ||
printf("%d %d\n",i,j); | ||
} ); | ||
std::cout << " after loop: " << *ii << " (should be " << num << ")" <<std::endl; | ||
std::cout << "---------- done test_atomic<int>_add -------------\n\n"; | ||
} | ||
|
||
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
|
||
void TestStdPar::test_atomicAdd_float(unsigned long N) { | ||
std::cout << "---------- test_atomicAdd_float -------------\n"; | ||
|
||
float ta[N]{0.}, tc[N]{0.}; | ||
for (int i=0; i<N; ++i) { | ||
ta[i%2] += i; | ||
tc[i] += i; | ||
} | ||
|
||
|
||
float *fa = new float[N]; | ||
float *fb = new float[N]; | ||
float *fc = new float[N]; | ||
std::for_each_n(std::execution::par_unseq, counting_iterator(0), N, | ||
[=] (int i) { | ||
fb[i%2] += i; | ||
#if defined ( _NVHPC_STDPAR_NONE ) || defined ( _NVHPC_STDPAR_MULTICORE ) | ||
fa[i % 2] += i; | ||
fc[i] += i; | ||
#else | ||
atomicAdd(&fa[i%2],float(i)); | ||
atomicAdd(&fc[i],float(i)); | ||
#endif | ||
}); | ||
for (int i=0; i<N; ++i) { | ||
printf("%d : %2g [%2g] %g %g [%g]\n",i, fa[i], ta[i], fb[i], fc[i], tc[i]); | ||
} | ||
std::cout << "---------- done test_atomicAdd_float -------------\n\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef TFCSSTDPARTEST_H | ||
#define TFCSSTDPARTEST_H 1 | ||
|
||
class TFCSStdParTest { | ||
public: | ||
void test(bool doAtomic, bool doVector, unsigned long num); | ||
}; | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "FastCaloSimAnalyzer/TFCSStdParTest.h" | ||
#include "FastCaloGpu/TestStdPar.h" | ||
|
||
#include <iostream> | ||
|
||
void TFCSStdParTest::test(bool doAtomic, bool doVector, unsigned long num) { | ||
|
||
TestStdPar tst; | ||
|
||
if (doAtomic) { | ||
tst.test_atomicAdd_int(num); | ||
tst.test_atomicAdd_float(num); | ||
} | ||
|
||
if (doVector) tst.test_vector(num); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <cstdio> | ||
#include <map> | ||
#include <iostream> | ||
#include "citer.h" | ||
#include <docopt/docopt.h> | ||
|
||
#include "FastCaloSimAnalyzer/TFCSStdParTest.h" | ||
|
||
static const char* USAGE = | ||
R"(Run test for stdpar | ||
Usage: | ||
runTFCSStdParTest [--doAtomicTest] [--doVectorTest] [-n <size> | --num <size>] | ||
runTFCSStdParTest (-h | --help) | ||
Options: | ||
-h --help Show help screen. | ||
--doAtomicTest Do test for atomic increments [default: false]. | ||
--doVectorTest Do test for allocating/accessing vector<float> [default: false]. | ||
-n <size> --num <size> Size of array to allocate for tests [default: 10]. | ||
)"; | ||
|
||
|
||
int main( int argc, char** argv ) { | ||
|
||
std::map<std::string, docopt::value> args = docopt::docopt( USAGE, {argv + 1, argv + argc}, true ); | ||
|
||
bool doAtomic = args["--doAtomicTest"].asBool(); | ||
bool doVec = args["--doVectorTest"].asBool(); | ||
int num = args["--num"].asLong(); | ||
|
||
TFCSStdParTest test; | ||
|
||
test.test(doAtomic,doVec,num); | ||
|
||
return 0; | ||
} |