int main() { /*! [create streams] */ clrngMrg31k3pStream* streams = clrngMrg31k3pCreateStreams(NULL, 2, NULL, NULL); clrngMrg31k3pStream* single = clrngMrg31k3pCreateStreams(NULL, 1, NULL, NULL); /*! [create streams] */ /*! [iterate streams] */ int count = 0; for (int i = 0; i < 100; i++) { double u = clrngMrg31k3pRandomU01(&streams[i % 2]); int x = clrngMrg31k3pRandomInteger(single, 1, 6); if (x * u < 2) count++; } /*! [iterate streams] */ /*! [output] */ printf("Average of indicators = %f\n", (double)count / 100.0); /*! [output] */ return 0; }
int main() { // prepare int replications = 1024; cl_double lambda = 50.0; clrngMrg31k3pStream* stream = clrngMrg31k3pCreateStreams(NULL, 1, NULL, NULL); // simulate cl_double sum = 0.0; for (int i = 0; i < replications; i++) sum += simulateOneRun(lambda, stream); printf("The average output is %.3f\n", sum / replications); // clean up clrngMrg31k3pDestroyStreams(stream); return 0; }
int main() { cl_double lambda = 50.0; clprobdistPoisson* dist = clprobdistPoissonCreate(50.0, NULL, NULL); clrngMrg31k3pStream* stream = clrngMrg31k3pCreateStreams(NULL, 1, NULL, NULL); for (int i = 0; i < 30; i++) { cl_double u = clrngMrg31k3pRandomU01(stream); cl_int with_param = clprobdistPoissonInverseCDF(lambda, u, NULL); cl_int with_object = clprobdistPoissonInverseCDFWithObject(dist, u, NULL); printf("u=%f, with param/obj=%d/%d %s\n", u, with_param, with_object, with_param == with_object ? "" : "<--"); } clprobdistPoissonDestroy(dist); clrngMrg31k3pDestroyStreams(stream); return 0; }
//************************************************************************ // Policies //************************************************************************ int one_Policy(cl_context context, cl_device_id device, cl_command_queue queue, void* data_) { OnePolicyData* data = (OnePolicyData*)data_; int n = data->n; int n1 = data->n1; int m = data->m; int s = data->s; int S = data->S; simResult * results = data->SimResults; ExecType execType = data->execType; //Declare streams & vars clrngMrg31k3pStream* stream_demand = NULL, *stream_order = NULL; clrngMrg31k3pStream *substreams_demand = NULL, *substreams_order = NULL; clrngStatus err; size_t streamBufferSize; size_t NbrStreams = ((execType == basic || execType == Case_a) ? 1 : n); //Create profit stat double *stat_profit = (double *)malloc(n * sizeof(double)); //Creator used to reset the state of the base seed in case there is successive calls to the same "Option" clrngMrg31k3pStreamCreator* Creator = clrngMrg31k3pCopyStreamCreator(NULL, &err); check_error(err, "%s(): cannot create stream creator", __func__); //Create stream demand if (execType == basic || execType == Case_a || execType == Case_b) { stream_demand = clrngMrg31k3pCreateStreams(Creator, NbrStreams, &streamBufferSize, &err); check_error(err, "%s(): cannot create random stream demand", __func__); } //************************* //Simulate on CPU if (execType == basic) { // in the document, this corresponds to the call to // inventorySimulateRunsOneStream() inventorySimulateRunsCPU(m, s, S, n, stream_demand, NULL, stat_profit, execType, results); } else if (execType == Case_a || execType == Case_b) { //Set the result object ExecOption if (results != NULL){ if (execType == Case_a) results->ExecOption = 2; else results->ExecOption = 3; } stream_order = clrngMrg31k3pCreateStreams(Creator, NbrStreams, &streamBufferSize, &err); check_error(err, "%s(): cannot create random stream order", __func__); // in the document, this corresponds to the calls to // inventorySimulateRunsManyStreams() // inventorySimulateRunsSubstreams() inventorySimulateRunsCPU(m, s, S, n, stream_demand, stream_order, stat_profit, execType, results); } //************************* //Simulate on Device if (execType != basic){ if (stream_demand != NULL) clrngMrg31k3pRewindStreams(NbrStreams, stream_demand); if (stream_order != NULL) clrngMrg31k3pRewindStreams(NbrStreams, stream_order); if (execType == Case_a) { if (results != NULL) (&results[3])->ExecOption = 2; printf("\n+++++++++ On Device (case a) : One policy, two streams with their substreams \n"); substreams_demand = clrngMrg31k3pMakeSubstreams(stream_demand, n, &streamBufferSize, &err); check_error(err, "%s(): cannot create random substreams demand", __func__); substreams_order = clrngMrg31k3pMakeSubstreams(stream_order, n, &streamBufferSize, &err); check_error(err, "%s(): cannot create random substreams order", __func__); inventorySimulateRunsGPU(context, device, queue, m, &s, &S, 1, n, 0, 0, OnePolicy, "inventorySimulateGPU", streamBufferSize, substreams_demand, substreams_order, stat_profit, (results != NULL?&results[3]:NULL)); } else if (execType == Case_b) { if (results != NULL) (&results[3])->ExecOption = 3; printf("\n+++++++++ On Device (case b) : One policy, two arrays of n streams each \n"); inventorySimulateRunsGPU(context, device, queue, m, &s, &S, 1, n, 0, 0, OnePolicy, "inventorySimulateGPU", streamBufferSize, stream_demand, stream_order, stat_profit, (results != NULL ? &results[3] : NULL)); } else if (execType == Case_c) { if (results != NULL) results->ExecOption = 4; printf("\n+++++++++ On Device (case c) : One policy, using 2*n1 streams with n2 substreams on each \n"); stream_demand = clrngMrg31k3pCreateStreams(Creator, n1, &streamBufferSize, &err); check_error(err, "%s(): cannot create random stream demand", __func__); stream_order = clrngMrg31k3pCreateStreams(Creator, n1, &streamBufferSize, &err); check_error(err, "%s(): cannot create random stream order", __func__); inventorySimulateRunsGPU(context, device, queue, m, &s, &S, 1, n, n1, n / n1, OnePolicy, "inventorySimulSubstreamsGPU", streamBufferSize, stream_demand, stream_order, stat_profit, results); } else if (execType == Case_d) { if (results != NULL) results->ExecOption = 5; printf("\n+++++++++ On Device (case d) : One policy, using 2*n streams with 2*n2 streams per Work item \n"); stream_demand = clrngMrg31k3pCreateStreams(Creator, n, &streamBufferSize, &err); check_error(err, "%s(): cannot create random stream demand", __func__); stream_order = clrngMrg31k3pCreateStreams(Creator, n, &streamBufferSize, &err); check_error(err, "%s(): cannot create random stream order", __func__); inventorySimulateRunsGPU(context, device, queue, m, &s, &S, 1, n, n1, n / n1, OnePolicy, "inventorySimul_DistinctStreams_GPU", streamBufferSize, stream_demand, stream_order, stat_profit, results); } } //Free resources free(stat_profit); clrngMrg31k3pDestroyStreams(stream_demand); clrngMrg31k3pDestroyStreams(stream_order); clrngMrg31k3pDestroyStreams(substreams_demand); clrngMrg31k3pDestroyStreams(substreams_order); clrngMrg31k3pDestroyStreamCreator(Creator); return EXIT_SUCCESS; }
int several_Policies(cl_context context, cl_device_id device, cl_command_queue queue, void* data_) { SeveralPoliciesData* data = (SeveralPoliciesData*)data_; int n = data->n; int n1 = data->n1; int m = data->m; int* s = data->s; int* S = data->S; int P = data->P; simResult * results = data->SimResults; ExecOption _optionType = data->optionType; //Declare streams & vars clrngMrg31k3pStream* streams_demand = NULL, *streams_order = NULL; double *stat_profit = NULL, *stat_diff = NULL; clrngStatus err; size_t streamBufferSize; //Allocate stat profit for n*P runs stat_profit = (double *)malloc(n * P * sizeof(double)); //Creator used to reset the state of the base seed in case there is successive calls to the same "Option" clrngMrg31k3pStreamCreator* Creator = clrngMrg31k3pCopyStreamCreator(NULL, &err); check_error(err, "%s(): cannot create stream creator", __func__); //Create n1 Streams for demand and order streams_demand = clrngMrg31k3pCreateStreams(Creator, n1, &streamBufferSize, &err); check_error(err, "%s(): cannot create random streams demand", __func__); streams_order = clrngMrg31k3pCreateStreams(Creator, n1, &streamBufferSize, &err); check_error(err, "%s(): cannot create random streams order", __func__); //printf("\n++++++++++++++++++++++++++++++ On Device +++++++++++++++++++++++++++++++\n"); if (_optionType == Option1) { printf("+++++++++ Simulate n2 runs on n1 work items using 2*n1 streams and n2 substreams for each, for P policies in series \n"); printf("+++++++++ CRN simulation : \n"); for (int k = 0; k < P; k++) { inventorySimulateRunsGPU(context, device, queue, m, &s[k], &S[k], P, n, n1, n/n1, Option1, "inventorySimulSubstreamsGPU", streamBufferSize, streams_demand, streams_order, &stat_profit[k*n], NULL); clrngMrg31k3pRewindStreams(n1, streams_demand); clrngMrg31k3pRewindStreams(n1, streams_order); } //Compute CI stat_diff = (double *)malloc(n * sizeof(double)); for (int i = 0; i < n; i++) stat_diff[i] = stat_profit[n + i] - stat_profit[i]; printf("\nDifference CRN :\n ------------\n"); computeCI(n, stat_diff, NULL); printf("\n+++++++++ IRN simulation : \n"); for (int k = 0; k < P; k++) { inventorySimulateRunsGPU(context, device, queue, m, &s[k], &S[k], P, n, n1, n / n1, Option1, "inventorySimulSubstreamsGPU", streamBufferSize, streams_demand, streams_order, &stat_profit[k*n], NULL); streams_demand = clrngMrg31k3pCreateStreams(NULL, n1, &streamBufferSize, &err); check_error(err, "%s(): cannot create random streams demand", __func__); streams_order = clrngMrg31k3pCreateStreams(NULL, n1, &streamBufferSize, &err); check_error(err, "%s(): cannot create random streams order", __func__); } //Compute CI for (int i = 0; i < n; i++) stat_diff[i] = stat_profit[n + i] - stat_profit[i]; printf("\nDifference IRN:\n ------------\n"); computeCI(n, stat_diff , NULL); } else { //Option2 if (results != NULL) results->ExecOption = 7; //printf("+++++++++ Simulate n2 runs on n1p workitmes using n1 streams and n2 substreams, all P policies in parallel\n"); inventorySimulateRunsGPU(context, device, queue, m, s, S, P, n * P, n1 * P, n/n1, Option2, "inventorySimulPoliciesGPU", streamBufferSize, streams_demand, streams_order, stat_profit, results); //Compute CI stat_diff = (double *)malloc(n * sizeof(double)); int n2 = n / n1; for (int j = 0; j < n2; j++) for (int i = 0; i < n1; i++) { int index = i + (j*n1*P); stat_diff[i + j*n1] = stat_profit[n1 + index] - stat_profit[index]; } //printf("\nDifference:\n ------------\n"); computeCI(n, stat_diff, results); } //Free Resources clrngMrg31k3pDestroyStreams(streams_demand); clrngMrg31k3pDestroyStreams(streams_order); clrngMrg31k3pDestroyStreamCreator(Creator); free(stat_diff); free(stat_profit); return EXIT_SUCCESS; }
int task(cl_context context, cl_device_id device, cl_command_queue queue, void* data_) { const TaskData* data = (const TaskData*) data_; cl_int err; if (data->points % data->points_per_work_item) check_error(CLQMC_INVALID_VALUE, "points must be a multiple of points_per_work_item"); if (data->replications % data->replications_per_work_item) check_error(CLQMC_INVALID_VALUE, "replications must be a multiple of replications_per_work_item"); // Lattice buffer size_t pointset_size; // gen_vec is given in common.c clqmcLatticeRule* pointset = clqmcLatticeRuleCreate(data->points, DIMENSION, gen_vec, &pointset_size, &err); check_error(err, NULL); cl_mem pointset_buf = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_HOST_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, pointset_size, pointset, &err); check_error(err, "cannot create point set buffer"); // Shifts buffer clqmc_fptype* shifts = (clqmc_fptype*) malloc(data->replications * DIMENSION * sizeof(clqmc_fptype)); // populate random shifts using a random stream clrngMrg31k3pStream* stream = clrngMrg31k3pCreateStreams(NULL, 1, NULL, &err); check_error(err, NULL); for (cl_uint i = 0; i < data->replications; i++) for (cl_uint j = 0; j < DIMENSION; j++) shifts[i * DIMENSION + j] = clrngMrg31k3pRandomU01(stream); err = clrngMrg31k3pDestroyStreams(stream); check_error(err, NULL); cl_mem shifts_buf = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_HOST_WRITE_ONLY | CL_MEM_COPY_HOST_PTR, data->replications * DIMENSION * sizeof(clqmc_fptype), shifts, &err); check_error(err, "cannot create shifts buffer"); // Output buffer size_t points_block_count = data->points / data->points_per_work_item; cl_mem output_buf = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, data->replications * points_block_count * sizeof(clqmc_fptype), NULL, &err); check_error(err, "cannot create output buffer"); // OpenCL kernel cl_program program = build_program_from_file(context, device, "client/DocsTutorial/example4_kernel.cl", NULL); check_error(err, NULL); cl_kernel kernel = clCreateKernel(program, "simulateWithRQMC", &err); check_error(err, "cannot create kernel"); int iarg = 0; err = clSetKernelArg(kernel, iarg++, sizeof(pointset_buf), &pointset_buf); err |= clSetKernelArg(kernel, iarg++, sizeof(shifts_buf), &shifts_buf); err |= clSetKernelArg(kernel, iarg++, sizeof(data->points_per_work_item), &data->points_per_work_item); err |= clSetKernelArg(kernel, iarg++, sizeof(data->replications), &data->replications); err |= clSetKernelArg(kernel, iarg++, sizeof(output_buf), &output_buf); check_error(err, "cannot set kernel arguments"); // Execution cl_event ev; size_t global_size = (data->replications / data->replications_per_work_item) * points_block_count; err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_size, NULL, 0, NULL, &ev); check_error(err, "cannot enqueue kernel"); err = clWaitForEvents(1, &ev); check_error(err, "error waiting for events"); clqmc_fptype* output = (clqmc_fptype*) malloc(data->replications * points_block_count * sizeof(clqmc_fptype)); err = clEnqueueReadBuffer(queue, output_buf, CL_TRUE, 0, data->replications * points_block_count * sizeof(clqmc_fptype), output, 0, NULL, NULL); check_error(err, "cannot read output buffer"); printf("\nAdvanced randomized quasi-Monte Carlo integration:\n\n"); err = clqmcLatticeRuleWriteInfo(pointset, stdout); check_error(err, NULL); printf("\n"); rqmcReport(data->replications, data->points, points_block_count, output); // Clean up clReleaseEvent(ev); clReleaseMemObject(output_buf); clReleaseMemObject(pointset_buf); clReleaseKernel(kernel); clReleaseProgram(program); free(output); err = clqmcLatticeRuleDestroy(pointset); check_error(err, NULL); return EXIT_SUCCESS; }