From 9e9a20c4af6e2ebc3355cc41d8db29795f25003a Mon Sep 17 00:00:00 2001 From: Uldis Locans Date: Wed, 15 Mar 2017 10:02:00 +0100 Subject: [PATCH] add tests for collimator physics and kick/push --- auto-tuning/CMakeLists.txt | 9 ++ auto-tuning/testCollimatorPhysics.cpp | 161 ++++++++++++++++++++++++++ auto-tuning/testPushKick.cpp | 132 +++++++++++++++++++++ 3 files changed, 302 insertions(+) create mode 100644 auto-tuning/testCollimatorPhysics.cpp create mode 100644 auto-tuning/testPushKick.cpp diff --git a/auto-tuning/CMakeLists.txt b/auto-tuning/CMakeLists.txt index 262c359..77e5f67 100644 --- a/auto-tuning/CMakeLists.txt +++ b/auto-tuning/CMakeLists.txt @@ -20,3 +20,12 @@ IF (ENABLE_MUSR) TARGET_LINK_LIBRARIES(testSearch dks ${Boost_LIBRARIES} ${CLFFT_LIBRARIES}) ENDIF (ENABLE_MUSR) +IF (ENABLE_OPAL) + ADD_EXECUTABLE(testCollimatorPhysics testCollimatorPhysics.cpp) + TARGET_LINK_LIBRARIES(testCollimatorPhysics dks ${Boost_LIBRARIES} ${CLFFT_LIBRARIES}) + + ADD_EXECUTABLE(testPushKick testPushKick.cpp) + TARGET_LINK_LIBRARIES(testPushKick dks ${Boost_LIBRARIES} ${CLFFT_LIBRARIES}) +ENDIF(ENABLE_OPAL) + + diff --git a/auto-tuning/testCollimatorPhysics.cpp b/auto-tuning/testCollimatorPhysics.cpp new file mode 100644 index 0000000..d800b05 --- /dev/null +++ b/auto-tuning/testCollimatorPhysics.cpp @@ -0,0 +1,161 @@ +#include +#include +#include + +#include "DKSOPAL.h" + +typedef struct { + int label; + unsigned localID; + double Rincol[3]; + double Pincol[3]; +} PART; + +PART initPartSmall(int d) { + + PART p; + p.label = 0; + p.localID = d; + + p.Rincol[0] = 0.0; + p.Rincol[1] = 0.0; + p.Rincol[2] = 0.02; + + p.Pincol[0] = 0.0; + p.Pincol[1] = 0.0; + p.Pincol[2] = 3.9920183237269791e-01; + + return p; +} + +void printPart(PART p) { + std::cout << "label: " << p.label << ", "; + std::cout << "localid: " << p.localID << ","; + std::cout << "Rincol: " << p.Rincol[0] << ", " << p.Rincol[1] << ", " << p.Rincol[2] << ", "; + std::cout << "Pincol: " << p.Pincol[0] << ", " << p.Pincol[1] << ", " << p.Pincol[2]; + std::cout << std::endl; +} + +void initParts(PART *p, int N) { + for (int i = 0; i < N; i++) + p[i] = initPartSmall(i); +} + +void printParts(PART *p, int N) { + for (int i = 0; i < N; i++) + printPart(p[i]); + std::cout << std::endl; +} + +void initParams(double *data) { + data[0] = 0.0;//2.0000000000000000e-02; + data[1] = 1.0;//1.0000000000000000e-02; + data[2] = 2.2100000000000000e+00; + data[3] = 6.0000000000000000e+00; + data[4] = 1.2010700000000000e+01; + data[5] = 2.6010000000000000e+00; + data[6] = 1.7010000000000000e+03; + data[7] = 1.2790000000000000e+03; + data[8] = 1.6379999999999999e-02; + data[9] = 1.9321266968325795e-01; + data[10] = 7.9000000000000000e+01; + data[11] = 1.0000000000000002e-12; +} + +int main(int argc, char *argv[]) { + + int loop = 10; + int numpart = 1e5; + char *api_name = new char[10]; + char *device_name = new char[10]; + strcpy(api_name, "Cuda"); + strcpy(device_name, "-gpu"); + + for (int i = 1; i < argc; i++) { + + if (argv[i] == std::string("-mic")) { + strcpy(api_name, "OpenMP"); + strcpy(device_name, "-mic"); + } + + if (argv[i] == std::string("-npart")) { + numpart = atoi(argv[i+1]); + i++; + } + + if (argv[i] == std::string("-loop")) { + loop = atoi(argv[i+1]); + i++; + } + + } + + std::cout << "=========================BEGIN TEST=========================" << std::endl; + std::cout << "Use api: " << api_name << "\t" << device_name << std::endl; + std::cout << "Number of particles: " << numpart << std::endl; + std::cout << "Number of loops: " << loop << std::endl; + std::cout << "------------------------------------------------------------" << std::endl; + + //init part vector to test mc + PART *parts = new PART[numpart]; + initParts(parts, numpart); + + double *params = new double[12]; + initParams(params); + + //init dks + int ierr; + DKSOPAL base; + base.setAPI(api_name, strlen(api_name)); + base.setDevice(device_name, strlen(api_name)); + ierr = base.initDevice(); + if (ierr != DKS_SUCCESS) + std::cout << "Error with init device!" << std::endl; + + //init random + base.callInitRandoms(numpart); + + //**test collimator physics and sort***// + void *part_ptr, *param_ptr; + + //allocate memory for particles + part_ptr = base.allocateMemory(numpart, ierr); + param_ptr = base.allocateMemory(12, ierr); + + //transfer data to device + base.writeData(part_ptr, parts, numpart); + base.writeData(param_ptr, params, 12); + + int numaddback; + base.callCollimatorPhysics2(part_ptr, param_ptr, numpart); + base.callCollimatorPhysicsSort(part_ptr, numpart, numaddback); + base.syncDevice(); + + //read data from device + base.readData(part_ptr, parts, numpart); + + //free memory + base.freeMemory(part_ptr, numpart); + base.freeMemory(param_ptr, 12); + + std::cout << std::fixed << std::setprecision(4); + for (int i = 0; i < 10; i++) { + std::cout << parts[i].label << "\t" + << parts[i].Rincol[0] << "\t" << parts[i].Rincol[1] << "\t" + << parts[i].Rincol[2] << "\t" << parts[i].Pincol[0] << "\t" + << parts[i].Pincol[1] << "\t" << parts[i].Pincol[2] << "\t" + << std::endl; + } + + std:: cout << "..." << std::endl; + + for (int i = numpart - 10; i < numpart; i++) { + std::cout << parts[i].label << "\t" + << parts[i].Rincol[0] << "\t" << parts[i].Rincol[1] << "\t" + << parts[i].Rincol[2] << "\t" << parts[i].Pincol[0] << "\t" + << parts[i].Pincol[1] << "\t" << parts[i].Pincol[2] << "\t" + << std::endl; + } + + return 0; +} diff --git a/auto-tuning/testPushKick.cpp b/auto-tuning/testPushKick.cpp new file mode 100644 index 0000000..1f2986d --- /dev/null +++ b/auto-tuning/testPushKick.cpp @@ -0,0 +1,132 @@ +#include +#include +#include + +#include "DKSOPAL.h" + +#include +#include "cuda_runtime.h" + +void initData(double3 *data, int N) { + for (int i = 0; i < N; i++) { + data[i].x = (double)rand() / RAND_MAX; + data[i].y = (double)rand() / RAND_MAX; + data[i].z = (double)rand() / RAND_MAX; + } +} + +void initDt(double *data, int N) { + for (int i = 0; i < N; i++) { + data[i] = 0.00001; + } +} + +int main(int argc, char *argv[]) { + + int loop = 10; + int numpart = 1e5; + char *api_name = new char[10]; + char *device_name = new char[10]; + strcpy(api_name, "Cuda"); + strcpy(device_name, "-gpu"); + + for (int i = 1; i < argc; i++) { + + if (argv[i] == std::string("-mic")) { + strcpy(api_name, "OpenMP"); + strcpy(device_name, "-mic"); + } + + if (argv[i] == std::string("-npart")) { + numpart = atoi(argv[i+1]); + i++; + } + + if (argv[i] == std::string("-loop")) { + loop = atoi(argv[i+1]); + i++; + } + + } + + std::cout << "=========================BEGIN TEST=========================" << std::endl; + std::cout << "Use api: " << api_name << "\t" << device_name << std::endl; + std::cout << "Number of particles: " << numpart << std::endl; + std::cout << "Number of loops: " << loop << std::endl; + std::cout << "------------------------------------------------------------" << std::endl; + + int ierr; + DKSOPAL dksbase; + dksbase.setAPI(api_name, strlen(api_name)); + dksbase.setDevice(device_name, strlen(api_name)); + ierr = dksbase.initDevice(); + if (ierr != DKS_SUCCESS) + std::cout << "Error with init device!" << std::endl; + + double3 *R = new double3[numpart]; + double3 *P = new double3[numpart]; + double3 *Ef = new double3[numpart]; + double3 *Bf = new double3[numpart]; + double *dt = new double[numpart]; + + initData(R, numpart); + initData(P, numpart); + initData(Ef, numpart); + initData(Bf, numpart); + initDt(dt, numpart); + + void *r_ptr, *p_ptr, *ef_ptr, *bf_ptr, *dt_ptr; + + r_ptr = dksbase.allocateMemory(numpart, ierr); + p_ptr = dksbase.allocateMemory(numpart, ierr); + ef_ptr = dksbase.allocateMemory(numpart, ierr); + bf_ptr = dksbase.allocateMemory(numpart, ierr); + dt_ptr = dksbase.allocateMemory(numpart, ierr); + + + dksbase.writeData(r_ptr, R, numpart); + dksbase.writeData(p_ptr, P, numpart); + dksbase.writeData(ef_ptr, Ef, numpart); + dksbase.writeData(bf_ptr, Bf, numpart); + dksbase.writeData(dt_ptr, dt, numpart); + + for (int i = 0; i < loop; ++i) + dksbase.callParallelTTrackerPush(r_ptr, p_ptr, dt_ptr, numpart, 1.0); + + + std::cout << std::fixed << std::setprecision(4); + for (int i = 0; i < 10; i++) + std::cout << R[i].x << "\t" << R[i].y << "\t" << R[i].z << std::endl; + + std:: cout << "..." << std::endl; + + for (int i = numpart - 10; i < numpart; i++) + std::cout << R[i].x << "\t" << R[i].y << "\t" << R[i].z << std::endl; + + std::cout << "============" << std::endl; + + dksbase.readData(r_ptr, R, numpart); + + std::cout << std::fixed << std::setprecision(4); + for (int i = 0; i < 10; i++) + std::cout << R[i].x << "\t" << R[i].y << "\t" << R[i].z << std::endl; + + std:: cout << "..." << std::endl; + + for (int i = numpart - 10; i < numpart; i++) + std::cout << R[i].x << "\t" << R[i].y << "\t" << R[i].z << std::endl; + + dksbase.freeMemory(r_ptr, numpart); + dksbase.freeMemory(p_ptr, numpart); + dksbase.freeMemory(ef_ptr, numpart); + dksbase.freeMemory(bf_ptr, numpart); + dksbase.freeMemory(dt_ptr, numpart); + + delete[] R; + delete[] P; + delete[] Ef; + delete[] Bf; + delete[] dt; + + +}