9 changed files with 462 additions and 87 deletions
@ -0,0 +1,67 @@ |
|||
#include <fstream> |
|||
|
|||
#include "MonteCarloTests.h" |
|||
#include "Common.h" |
|||
|
|||
CTG::CTG(size_t _randomNumbers, size_t _threadCount) |
|||
{ |
|||
randomNumbers = _randomNumbers; |
|||
threadCount = _threadCount; |
|||
randomBuffer = std::vector<float>(threadCount * randomNumbers); |
|||
seedBuffer = std::vector<float>(threadCount); |
|||
kernel_name = "randomCTG"; |
|||
|
|||
for (int i = 0; i < threadCount; ++i) |
|||
{ |
|||
seedBuffer[i] = rand(); |
|||
} |
|||
} |
|||
|
|||
void CTG::collect_results(cl::CommandQueue* queue) { |
|||
|
|||
} |
|||
|
|||
void CTG::gpu_compute(cl::Context* context, cl::CommandQueue* queue, cl::Program* program, cl::Event* Event) { |
|||
cl_int err = CL_SUCCESS; |
|||
cl::Kernel kernel = cl::Kernel(*program, kernel_name.c_str(), &err); |
|||
CheckCLError(err); |
|||
|
|||
clInputBuffer = cl::Buffer(*context, CL_MEM_READ_ONLY, sizeof(float) * threadCount, NULL, &err); |
|||
queue->enqueueWriteBuffer(clInputBuffer, true, 0, sizeof(float) * threadCount, seedBuffer.data()); |
|||
|
|||
clResultBuffer = cl::Buffer(*context, CL_MEM_WRITE_ONLY, sizeof(float) * threadCount * randomNumbers, NULL, &err); |
|||
|
|||
// Set the kernel parameters
|
|||
kernel.setArg(0, randomNumbers); |
|||
kernel.setArg(1, clInputBuffer); |
|||
kernel.setArg(2, clResultBuffer); |
|||
|
|||
// Enqueue the kernel: threadCount threads in total, each generating random numbers in [0,1] randomNumbers times
|
|||
queue->enqueueNDRangeKernel(kernel, |
|||
cl::NullRange, |
|||
cl::NDRange(threadCount, 1), |
|||
cl::NullRange, |
|||
NULL, |
|||
Event); |
|||
} |
|||
|
|||
void CTG::cpu_compute() |
|||
{ |
|||
} |
|||
|
|||
bool CTG::validate_results() |
|||
{ |
|||
std::ofstream ofs("randoms_" + description() + ".txt", std::ofstream::out); |
|||
for (int i = 0; i < threadCount; ++i) { |
|||
for (int j = 0; j < randomNumbers; ++j) { |
|||
ofs << j << " " << randomBuffer[i + j * threadCount] << std::endl; |
|||
} |
|||
} |
|||
ofs.close(); |
|||
return true; |
|||
} |
|||
|
|||
std::string CTG::description() |
|||
{ |
|||
return kernel_name + "(numbers=" + std::to_string(randomNumbers) + ",threads=" + std::to_string(threadCount) + ")"; |
|||
} |
@ -0,0 +1,60 @@ |
|||
#include <fstream> |
|||
|
|||
#include "MonteCarloTests.h" |
|||
#include "Common.h" |
|||
|
|||
Halton::Halton(size_t _randomNumbers, size_t _threadCount, size_t _base) { |
|||
randomNumbers = _randomNumbers; |
|||
threadCount = _threadCount; |
|||
base = _base; |
|||
randomBuffer = std::vector<float>(threadCount * randomNumbers); |
|||
kernel_name = "haltonSequence"; |
|||
} |
|||
|
|||
void Halton::collect_results(cl::CommandQueue* queue) |
|||
{ |
|||
queue->enqueueReadBuffer(clResultBuffer, true, 0, sizeof(float) * threadCount * randomNumbers, randomBuffer.data()); |
|||
} |
|||
|
|||
void Halton::gpu_compute(cl::Context* context, cl::CommandQueue* queue, cl::Program* program, cl::Event* Event) |
|||
{ |
|||
cl_int err = CL_SUCCESS; |
|||
cl::Kernel kernel = cl::Kernel(*program, kernel_name.c_str(), &err); |
|||
CheckCLError(err); |
|||
|
|||
clResultBuffer = cl::Buffer(*context, CL_MEM_WRITE_ONLY, sizeof(float) * threadCount * randomNumbers, NULL, &err); |
|||
|
|||
// Set the kernel parameters
|
|||
kernel.setArg(0, randomNumbers); |
|||
kernel.setArg(1, base); |
|||
kernel.setArg(2, clResultBuffer); |
|||
|
|||
// Enqueue the kernel: threadCount threads in total, each generating random numbers in [0,1] randomNumbers times
|
|||
queue->enqueueNDRangeKernel(kernel, |
|||
cl::NullRange, |
|||
cl::NDRange(threadCount, 1), |
|||
cl::NullRange, |
|||
NULL, |
|||
Event); |
|||
} |
|||
|
|||
void Halton::cpu_compute() |
|||
{ |
|||
} |
|||
|
|||
bool Halton::validate_results() |
|||
{ |
|||
std::ofstream ofs("randoms_" + description() + ".txt", std::ofstream::out); |
|||
for (int i = 0; i < threadCount; ++i) { |
|||
for (int j = 0; j < randomNumbers; ++j) { |
|||
ofs << j << " " << randomBuffer[i + j * threadCount] << std::endl; |
|||
} |
|||
} |
|||
ofs.close(); |
|||
return true; |
|||
} |
|||
|
|||
std::string Halton::description() |
|||
{ |
|||
return kernel_name + "(numbers=" + std::to_string(randomNumbers) + ",threads=" + std::to_string(threadCount) + ",base=" + std::to_string(base) + ")"; |
|||
} |
@ -0,0 +1,69 @@ |
|||
#include <fstream> |
|||
|
|||
#include "MonteCarloTests.h" |
|||
#include "Common.h" |
|||
|
|||
Hybrid::Hybrid(size_t _randomNumbers, size_t _threadCount) |
|||
{ |
|||
randomNumbers = _randomNumbers; |
|||
threadCount = _threadCount; |
|||
randomBuffer = std::vector<float>(threadCount * randomNumbers); |
|||
seedBuffer = std::vector<float>(threadCount); |
|||
kernel_name = "hybridRNG"; |
|||
|
|||
for (int i = 0; i < threadCount; ++i) |
|||
{ |
|||
seedBuffer[i] = rand(); |
|||
} |
|||
} |
|||
|
|||
void Hybrid::collect_results(cl::CommandQueue* queue) |
|||
{ |
|||
queue->enqueueReadBuffer(clResultBuffer, true, 0, sizeof(float) * threadCount * randomNumbers, randomBuffer.data()); |
|||
} |
|||
|
|||
void Hybrid::gpu_compute(cl::Context* context, cl::CommandQueue* queue, cl::Program* program, cl::Event* Event) |
|||
{ |
|||
cl_int err = CL_SUCCESS; |
|||
cl::Kernel kernel = cl::Kernel(*program, kernel_name.c_str(), &err); |
|||
CheckCLError(err); |
|||
|
|||
clInputBuffer = cl::Buffer(*context, CL_MEM_READ_ONLY, sizeof(float) * threadCount, NULL, &err); |
|||
queue->enqueueWriteBuffer(clInputBuffer, true, 0, sizeof(float) * threadCount, seedBuffer.data()); |
|||
|
|||
clResultBuffer = cl::Buffer(*context, CL_MEM_WRITE_ONLY, sizeof(float) * threadCount * randomNumbers, NULL, &err); |
|||
|
|||
// Set the kernel parameters
|
|||
kernel.setArg(0, randomNumbers); |
|||
kernel.setArg(1, clInputBuffer); |
|||
kernel.setArg(2, clResultBuffer); |
|||
|
|||
// Enqueue the kernel: threadCount threads in total, each generating random numbers in [0,1] randomNumbers times
|
|||
queue->enqueueNDRangeKernel(kernel, |
|||
cl::NullRange, |
|||
cl::NDRange(threadCount, 1), |
|||
cl::NullRange, |
|||
NULL, |
|||
Event); |
|||
} |
|||
|
|||
void Hybrid::cpu_compute() |
|||
{ |
|||
} |
|||
|
|||
bool Hybrid::validate_results() |
|||
{ |
|||
std::ofstream ofs("randoms_" + description() + ".txt", std::ofstream::out); |
|||
for (int i = 0; i < threadCount; ++i) { |
|||
for (int j = 0; j < randomNumbers; ++j) { |
|||
ofs << j << " " << randomBuffer[i + j * threadCount] << std::endl; |
|||
} |
|||
} |
|||
ofs.close(); |
|||
return true; |
|||
} |
|||
|
|||
std::string Hybrid::description() |
|||
{ |
|||
return kernel_name + "(numbers=" + std::to_string(randomNumbers) + ",threads=" + std::to_string(threadCount) + ")"; |
|||
} |
@ -0,0 +1,69 @@ |
|||
#include <fstream> |
|||
|
|||
#include "MonteCarloTests.h" |
|||
#include "Common.h" |
|||
|
|||
LCG::LCG(size_t _randomNumbers, size_t _threadCount) |
|||
{ |
|||
randomNumbers = _randomNumbers; |
|||
threadCount = _threadCount; |
|||
randomBuffer = std::vector<float>(threadCount * randomNumbers); |
|||
seedBuffer = std::vector<float>(threadCount); |
|||
kernel_name = "randomLCG"; |
|||
|
|||
for (int i = 0; i < threadCount; ++i) |
|||
{ |
|||
seedBuffer[i] = rand(); |
|||
} |
|||
} |
|||
|
|||
void LCG::collect_results(cl::CommandQueue* queue) |
|||
{ |
|||
queue->enqueueReadBuffer(clResultBuffer, true, 0, sizeof(float) * threadCount * randomNumbers, randomBuffer.data()); |
|||
} |
|||
|
|||
void LCG::gpu_compute(cl::Context* context, cl::CommandQueue* queue, cl::Program* program, cl::Event* Event) |
|||
{ |
|||
cl_int err = CL_SUCCESS; |
|||
cl::Kernel kernel = cl::Kernel(*program, kernel_name.c_str(), &err); |
|||
CheckCLError(err); |
|||
|
|||
clInputBuffer = cl::Buffer(*context, CL_MEM_READ_ONLY, sizeof(float) * threadCount, NULL, &err); |
|||
queue->enqueueWriteBuffer(clInputBuffer, true, 0, sizeof(float) * threadCount, seedBuffer.data()); |
|||
|
|||
clResultBuffer = cl::Buffer(*context, CL_MEM_WRITE_ONLY, sizeof(float) * threadCount * randomNumbers, NULL, &err); |
|||
|
|||
// Set the kernel parameters
|
|||
kernel.setArg(0, randomNumbers); |
|||
kernel.setArg(1, clInputBuffer); |
|||
kernel.setArg(2, clResultBuffer); |
|||
|
|||
// Enqueue the kernel: threadCount threads in total, each generating random numbers in [0,1] randomNumbers times
|
|||
queue->enqueueNDRangeKernel(kernel, |
|||
cl::NullRange, |
|||
cl::NDRange(threadCount, 1), |
|||
cl::NullRange, |
|||
NULL, |
|||
Event); |
|||
} |
|||
|
|||
void LCG::cpu_compute() |
|||
{ |
|||
} |
|||
|
|||
bool LCG::validate_results() |
|||
{ |
|||
std::ofstream ofs("randoms_" + description() + ".txt", std::ofstream::out); |
|||
for (int i = 0; i < threadCount; ++i) { |
|||
for (int j = 0; j < randomNumbers; ++j) { |
|||
ofs << j << " " << randomBuffer[i + j * threadCount] << std::endl; |
|||
} |
|||
} |
|||
ofs.close(); |
|||
return true; |
|||
} |
|||
|
|||
std::string LCG::description() |
|||
{ |
|||
return kernel_name + "(numbers=" + std::to_string(randomNumbers) +",threads="+std::to_string(threadCount) + ")"; |
|||
} |
@ -0,0 +1,74 @@ |
|||
#include <fstream> |
|||
|
|||
#include "MonteCarloTests.h" |
|||
#include "Common.h" |
|||
|
|||
LFG::LFG(size_t _randomNumbers, size_t _threadCount, size_t _randomStateSize) |
|||
{ |
|||
randomNumbers = _randomNumbers; |
|||
threadCount = _threadCount; |
|||
randomBuffer = std::vector<float>(threadCount * randomNumbers); |
|||
seedBuffer = std::vector<float>(threadCount); |
|||
kernel_name = "randomLFG"; |
|||
randomStateSize = _randomStateSize; |
|||
|
|||
for (int i = 0; i < threadCount; ++i) |
|||
{ |
|||
seedBuffer[i] = rand(); |
|||
} |
|||
} |
|||
|
|||
void LFG::collect_results(cl::CommandQueue* queue) |
|||
{ |
|||
queue->enqueueReadBuffer(clResultBuffer, true, 0, sizeof(float) * threadCount * randomNumbers, randomBuffer.data()); |
|||
} |
|||
|
|||
void LFG::gpu_compute(cl::Context* context, cl::CommandQueue* queue, cl::Program* program, cl::Event* Event) |
|||
{ |
|||
cl_int err = CL_SUCCESS; |
|||
cl::Kernel kernel = cl::Kernel(*program, kernel_name.c_str(), &err); |
|||
CheckCLError(err); |
|||
|
|||
clInputBuffer = cl::Buffer(*context, CL_MEM_READ_ONLY, sizeof(float) * threadCount, NULL, &err); |
|||
queue->enqueueWriteBuffer(clInputBuffer, true, 0, sizeof(float) * threadCount, seedBuffer.data()); |
|||
|
|||
clResultBuffer = cl::Buffer(*context, CL_MEM_WRITE_ONLY, sizeof(float) * threadCount * randomNumbers, NULL, &err); |
|||
cl::Buffer randomStates(*context, CL_MEM_WRITE_ONLY, sizeof(float) * threadCount * randomStateSize, NULL, &err); |
|||
|
|||
// Set the kernel parameters
|
|||
kernel.setArg(0, randomNumbers); |
|||
kernel.setArg(1, clInputBuffer); |
|||
kernel.setArg(2, randomStateSize); |
|||
kernel.setArg(3, randomStates); |
|||
kernel.setArg(4, clResultBuffer); |
|||
|
|||
// Enqueue the kernel: threadCount threads in total, each generating random numbers in [0,1] randomNumbers times
|
|||
queue->enqueueNDRangeKernel(kernel, |
|||
cl::NullRange, |
|||
cl::NDRange(threadCount, 1), |
|||
cl::NullRange, |
|||
NULL, |
|||
Event); |
|||
} |
|||
|
|||
void LFG::cpu_compute() |
|||
{ |
|||
} |
|||
|
|||
bool LFG::validate_results() |
|||
{ |
|||
std::ofstream ofs("randoms_" + description() + ".txt", std::ofstream::out); |
|||
for (int i = 0; i < threadCount; ++i) { |
|||
for (int j = 0; j < randomNumbers; ++j) { |
|||
ofs << j << " " << randomBuffer[i + j * threadCount] << std::endl; |
|||
} |
|||
} |
|||
ofs.close(); |
|||
return true; |
|||
} |
|||
|
|||
std::string LFG::description() |
|||
{ |
|||
return kernel_name + "(numbers=" + std::to_string(randomNumbers) + ",threads=" + std::to_string(threadCount) + ",state="+std::to_string(randomStateSize) + ")"; |
|||
} |
|||
|
@ -0,0 +1,84 @@ |
|||
#pragma once |
|||
#include "Tests.h" |
|||
|
|||
|
|||
class LCG : public TestCase { |
|||
private: |
|||
size_t randomNumbers; |
|||
size_t threadCount; |
|||
std::vector<float> randomBuffer; |
|||
std::vector<float> seedBuffer; |
|||
std::string kernel_name; |
|||
public: |
|||
LCG(size_t _randomNumbers, size_t _threadCount); |
|||
void collect_results(cl::CommandQueue* queue); |
|||
void gpu_compute(cl::Context* context, cl::CommandQueue* queue, cl::Program* program, cl::Event* Event); |
|||
void cpu_compute(); |
|||
bool validate_results(); |
|||
std::string description(); |
|||
}; |
|||
|
|||
class LFG : public TestCase { |
|||
private: |
|||
size_t randomNumbers; |
|||
size_t threadCount; |
|||
std::vector<float> randomBuffer; |
|||
std::vector<float> seedBuffer; |
|||
std::string kernel_name; |
|||
size_t randomStateSize; |
|||
public: |
|||
LFG(size_t _randomNumbers, size_t _threadCount, size_t _randomStateSize); |
|||
void collect_results(cl::CommandQueue* queue); |
|||
void gpu_compute(cl::Context* context, cl::CommandQueue* queue, cl::Program* program, cl::Event* Event); |
|||
void cpu_compute(); |
|||
bool validate_results(); |
|||
std::string description(); |
|||
}; |
|||
|
|||
class CTG : public TestCase { |
|||
private: |
|||
size_t randomNumbers; |
|||
size_t threadCount; |
|||
std::vector<float> randomBuffer; |
|||
std::vector<float> seedBuffer; |
|||
std::string kernel_name; |
|||
public: |
|||
CTG(size_t _randomNumbers, size_t _threadCount); |
|||
void collect_results(cl::CommandQueue* queue); |
|||
void gpu_compute(cl::Context* context, cl::CommandQueue* queue, cl::Program* program, cl::Event* Event); |
|||
void cpu_compute(); |
|||
bool validate_results(); |
|||
std::string description(); |
|||
}; |
|||
|
|||
class Hybrid : public TestCase { |
|||
private: |
|||
size_t randomNumbers; |
|||
size_t threadCount; |
|||
std::vector<float> randomBuffer; |
|||
std::vector<float> seedBuffer; |
|||
std::string kernel_name; |
|||
public: |
|||
Hybrid(size_t _randomNumbers, size_t _threadCount); |
|||
void collect_results(cl::CommandQueue* queue); |
|||
void gpu_compute(cl::Context* context, cl::CommandQueue* queue, cl::Program* program, cl::Event* Event); |
|||
void cpu_compute(); |
|||
bool validate_results(); |
|||
std::string description(); |
|||
}; |
|||
|
|||
class Halton : public TestCase { |
|||
private: |
|||
size_t randomNumbers; |
|||
size_t threadCount; |
|||
size_t base; |
|||
std::vector<float> randomBuffer; |
|||
std::string kernel_name; |
|||
public: |
|||
Halton(size_t _randomNumbers, size_t _threadCount, size_t _base); |
|||
void collect_results(cl::CommandQueue* queue); |
|||
void gpu_compute(cl::Context* context, cl::CommandQueue* queue, cl::Program* program, cl::Event* Event); |
|||
void cpu_compute(); |
|||
bool validate_results(); |
|||
std::string description(); |
|||
}; |
Loading…
Reference in new issue