├── LICENSE ├── README.md ├── doc ├── Makefile ├── clProbDist.bib └── clProbDist.doxy └── src ├── CMakeLists.txt ├── FindOpenCL.cmake ├── client ├── AsianOption │ ├── AsianOption.c │ ├── AsianOptionKernel.cl │ ├── Simulate_Runs.c │ ├── Simulate_Runs.h │ └── Types.h ├── CMakeLists.txt ├── DocsTutorial │ ├── example1.c │ ├── example2.c │ ├── example3.c │ ├── example4.c │ └── example4_kernel.cl ├── Inventory │ ├── InventoryKernels.cl │ ├── Policies.c │ ├── Policies.h │ ├── SimulateRuns.c │ ├── SimulateRuns.h │ ├── Types.h │ ├── inventory.c │ └── test.c ├── Performance │ ├── Options.c │ ├── Options.h │ ├── PerformanceKernels.cl │ ├── SimulateRun.c │ ├── SimulateRun.h │ └── performance.c ├── common.c ├── common.h └── selfcontained.c ├── include └── clProbDist │ ├── clProbDist.clh │ ├── clProbDist.h │ ├── clProbDist.version.h.in │ ├── clProbDist_template.h │ ├── continuous.clh │ ├── continuous.h │ ├── exponential.clh │ ├── exponential.h │ ├── gamma.clh │ ├── gamma.h │ ├── lognormal.clh │ ├── lognormal.h │ ├── normal.clh │ ├── normal.h │ ├── poisson.clh │ ├── poisson.h │ └── private │ ├── continuous.c.h │ ├── exponential.c.h │ ├── gamma.c.h │ ├── lognormal.c.h │ ├── normal.c.h │ └── poisson.c.h ├── library ├── CMakeLists.txt ├── clProbDist.c ├── clProbDist.pc.in ├── clProbDistConfig.cmake.in ├── continuous.c ├── exponential.c ├── gamma.c ├── lognormal.c ├── normal.c ├── poisson.c ├── private.c └── private.h └── tests ├── CMakeLists.txt └── ctest ├── checks.c.h ├── checks.h ├── cli.c ├── dispatch.c.h ├── exponential_checks.c ├── exponential_dispatch.c ├── gamma_checks.c ├── gamma_dispatch.c ├── lognormal_checks.c ├── lognormal_dispatch.c ├── mangle.h ├── normal_checks.c ├── normal_dispatch.c ├── poisson_checks.c ├── poisson_dispatch.c ├── util.c └── util.h /README.md: -------------------------------------------------------------------------------- 1 | # clProbDist Library 2 | 3 | A library for probability distributions in OpenCL. 4 | 5 | 6 | ## Overview 7 | 8 | The clProbDist library provides facilities for evaluating, on OpenCL devices and on hosts: 9 | 10 | - probability density functions; 11 | - probability mass functions; 12 | - cumulative distribution functions; 13 | - inverse cumulative distribution functions; and 14 | - reliability functions. 15 | 16 | Five distributions are currently supported: 17 | 18 | - normal; 19 | - lognormal; 20 | - exponential; 21 | - gamma; and 22 | - Poisson. 23 | 24 | In its current state, clProbDist defines a framework for working with 25 | probability distributions in OpenCL, and to show some working examples. Dozens 26 | of **additional distributions need to be contributed** to make clProbdist a 27 | mature library. 28 | 29 | 30 | ## Documentation 31 | 32 | The 33 | [clProbDist documentation](http://umontreal-simul.github.io/clProbDist/htmldocs/index.html) 34 | includes: 35 | 36 | - [a reference for the implemented distributions](http://umontreal-simul.github.io/clProbDist/htmldocs/index.html#distributions); 37 | - [basic usage examples](http://umontreal-simul.github.io/clProbDist/htmldocs/index.html#basic_usage) including the [generation of nonuniform variates](http://umontreal-simul.github.io/clProbDist/htmldocs/index.html#example_poissongen) with the help of the [clRNG library](https://github.com/clMathLibraries/clRNG); and 38 | - [the API reference](http://umontreal-simul.github.io/clProbDist/htmldocs/clProbDist__template_8h.html). 39 | 40 | 41 | ## Examples 42 | 43 | Examples can be found in `src/client`. 44 | The compiled client program examples can be found under the `bin` subdirectory 45 | of the installation package (`$CLPROBDIST_ROOT/bin` under Linux). 46 | 47 | 48 | ## Simple example 49 | 50 | The simple example below shows how to use clProbDist to compute the inverse CDF 51 | of the exponential distribution using device side headers (`.clh`) in your 52 | OpenCL kernel. 53 | Note that the example expects an OpenCL GPU device, that supports 54 | double-precision floating point computations, to be available. 55 | This example uses the API that uses *distribution objects*; another API that uses distribution parameters instead is also available. 56 | 57 | ```c 58 | #include 59 | #include 60 | #include 61 | 62 | #include 63 | #include 64 | 65 | 66 | int main( void ) 67 | { 68 | cl_int err; 69 | cl_platform_id platform = 0; 70 | cl_device_id device = 0; 71 | cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 }; 72 | cl_context ctx = 0; 73 | cl_command_queue queue = 0; 74 | cl_program program = 0; 75 | cl_kernel kernel = 0; 76 | cl_event event = 0; 77 | cl_mem bufIn, bufOut; 78 | double *out; 79 | const char *includes; 80 | char buildLog[4096]; 81 | size_t numWorkItems = 64; 82 | size_t i; 83 | clprobdistExponential *dist = 0; 84 | size_t distBufferSize = 0; 85 | size_t kernelLines = 0; 86 | 87 | /* Sample kernel that calls clProbDist device-side interfaces to compute the 88 | inverse CDF of the exponential distribution at different quantiles */ 89 | const char *kernelSrc[] = { 90 | "#include \n", 91 | " \n", 92 | "__kernel void example(__global const clprobdistExponential *dist, \n", 93 | " __global double *out) \n", 94 | "{ \n", 95 | " int gid = get_global_id(0); \n", 96 | " int gsize = get_global_size(0); \n", 97 | " double quantile = (gid + 0.5) / gsize; \n", 98 | " out[gid] = clprobdistExponentialInverseCDFWithObject( \n", 99 | " dist, quantile, (void *)0); \n", 100 | "} \n", 101 | }; 102 | 103 | /* Setup OpenCL environment. */ 104 | err = clGetPlatformIDs( 1, &platform, NULL ); 105 | err = clGetDeviceIDs( platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL ); // FIXME 106 | err = clGetDeviceIDs( platform, CL_DEVICE_TYPE_CPU, 1, &device, NULL ); 107 | 108 | props[1] = (cl_context_properties)platform; 109 | ctx = clCreateContext( props, 1, &device, NULL, NULL, &err ); 110 | #ifdef CL_VERSION_2_0 111 | queue = clCreateCommandQueueWithProperties( ctx, device, (cl_queue_properties[]){0}, &err ); 112 | #else 113 | queue = clCreateCommandQueue( ctx, device, 0, &err ); 114 | #endif 115 | 116 | 117 | /* Make sure CLPROBDIST_ROOT is specified to get library path */ 118 | includes = clprobdistGetLibraryDeviceIncludes(&err); 119 | if(err != CL_SUCCESS) printf("\n%s\nSpecify environment variable CLPROBDIST_ROOT as described\n", clprobdistGetErrorString()); 120 | 121 | /* Create sample kernel */ 122 | kernelLines = sizeof(kernelSrc) / sizeof(kernelSrc[0]); 123 | program = clCreateProgramWithSource(ctx, kernelLines, kernelSrc, NULL, &err); 124 | err = clBuildProgram(program, 1, &device, includes, NULL, NULL); 125 | if(err != CL_SUCCESS) 126 | { 127 | printf("\nclBuildProgram has failed\n"); 128 | clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 4096, buildLog, NULL); 129 | printf("%s", buildLog); 130 | exit(1); 131 | } 132 | kernel = clCreateKernel(program, "example", &err); 133 | 134 | /* Create an exponential distribution with unit mean */ 135 | dist = clprobdistExponentialCreate(1.0, &distBufferSize, (clprobdistStatus *)&err); 136 | 137 | /* Create buffers for the kernel */ 138 | bufIn = clCreateBuffer(ctx, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, distBufferSize, dist, &err); 139 | bufOut = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, numWorkItems * sizeof(cl_double), NULL, &err); 140 | 141 | /* Setup the kernel */ 142 | err = clSetKernelArg(kernel, 0, sizeof(bufIn), &bufIn); 143 | err = clSetKernelArg(kernel, 1, sizeof(bufOut), &bufOut); 144 | 145 | /* Execute the kernel and read back results */ 146 | err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &numWorkItems, NULL, 0, NULL, &event); 147 | err = clWaitForEvents(1, &event); 148 | out = (double *)malloc(numWorkItems * sizeof(out[0])); 149 | err = clEnqueueReadBuffer(queue, bufOut, CL_TRUE, 0, numWorkItems * sizeof(out[0]), out, 0, NULL, NULL); 150 | 151 | /* Display results and check that the original quantile is recovered by evaluating the CDF */ 152 | for (i = 0; i < numWorkItems; i++) 153 | printf("quantile %.3f : %9.3e -> %.3f\n", (i + 0.5) / numWorkItems, out[i], clprobdistExponentialCDFWithObject(dist, out[i], &err)); 154 | 155 | /* Release allocated resources */ 156 | clReleaseEvent(event); 157 | free(out); 158 | clReleaseMemObject(bufIn); 159 | clReleaseMemObject(bufOut); 160 | 161 | clReleaseKernel(kernel); 162 | clReleaseProgram(program); 163 | 164 | clReleaseCommandQueue(queue); 165 | clReleaseContext(ctx); 166 | 167 | return 0; 168 | } 169 | ``` 170 | 171 | 172 | ## Acknowledgments 173 | 174 | clProbDist was developed by Nabil Kemerchou, David Munger and Pierre L'Ecuyer at 175 | Université de Montréal, in collaboration with Advanced Micro Devices, Inc. 176 | -------------------------------------------------------------------------------- /doc/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all html latex install clean 2 | 3 | 4 | all: html latex 5 | 6 | html: 7 | doxygen clProbDist.doxy 8 | 9 | latex: html 10 | $(MAKE) -C out/latex 11 | 12 | install: html latex 13 | mkdir -p "$(PREFIX)/doc" 14 | install -d out/html "$(PREFIX)/doc" 15 | install --mode=0644 out/latex/refman.pdf "$(PREFIX)/doc" 16 | 17 | clean: 18 | rm -rf out/html out/latex 19 | -------------------------------------------------------------------------------- /doc/clProbDist.bib: -------------------------------------------------------------------------------- 1 | @ARTICLE {tBHA70a, 2 | AUTHOR="G. P. Bhattacharjee", 3 | TITLE="The incomplete gamma integral", 4 | JOURNAL={Applied Statistics}, 5 | NOTE={AS32}, 6 | VOLUME={19}, 7 | YEAR={1970}, 8 | PAGES={285--287} } 9 | 10 | @ARTICLE {tBLA76a, 11 | OLD = {tBEJ76a}, 12 | AUTHOR="J. M. Blair and C. A. Edwards and J. H. Johnson", 13 | TITLE="Rational {C}hebyshev Approximations for the Inverse of the 14 | Error Function", 15 | JOURNAL={Mathematics of Computation}, 16 | VOLUME={30}, 17 | YEAR={1976}, 18 | PAGES={827--830} } 19 | 20 | @BOOK {sBRA87a, 21 | AUTHOR="P. Bratley and B. L. Fox and L. E. Schrage", 22 | TITLE={A Guide to Simulation}, 23 | EDITION={Second}, 24 | PUBLISHER={Springer-Verlag}, 25 | ADDRESS={New York, NY}, 26 | YEAR={1987} } 27 | 28 | @article{iBRE71a, 29 | author = {R. P. Brent}, 30 | title = {An algorithm with guaranteed convergence for 31 | finding a zero of a function}, 32 | journal = {Computer Journal}, 33 | year = {1971}, 34 | volume = {14}, 35 | optnote = {Brent-Dekker method}, 36 | pages = {422--425}, 37 | } 38 | 39 | @book{iBRE73a, 40 | author = {R. P. Brent}, 41 | title = {Algorithms for Minimization without Derivatives}, 42 | publisher = {Prentice-Hall}, 43 | address = {Englewood Cliffs, NJ}, 44 | year = {1973}, 45 | } 46 | 47 | @ARTICLE {rCHE74a, 48 | AUTHOR="H. C. Chen and Y. Asau", 49 | YEAR={1974}, 50 | TITLE="On Generating Random Variates From an Empirical Distribution", 51 | JOURNAL={{AIEE} Transactions}, 52 | VOLUME={6}, 53 | PAGES={163--166} } 54 | 55 | @BOOK {rDEV86a, 56 | AUTHOR="L. Devroye", 57 | TITLE={Non-Uniform Random Variate Generation}, 58 | PUBLISHER="Springer-Verlag", 59 | ADDRESS={New York, NY}, 60 | YEAR={1986} } 61 | 62 | @manual{iLEC08j, 63 | author = {P. L'Ecuyer}, 64 | title = {{SSJ}: A {J}ava Library for Stochastic Simulation}, 65 | note = {Software {user's} guide, 66 | available at \url{http://www.iro.umontreal.ca/~lecuyer}}, 67 | institution = {D\'epartement d'Informatique et de Recherche 68 | Op\'erationnelle, Universit\'e de Montr\'eal}, 69 | year = {2008}, 70 | OPTannote = {} 71 | } 72 | 73 | @ARTICLE {tSCH78a, 74 | AUTHOR="J. L. Schonfelder", 75 | TITLE="Chebyshev Expansions for the Error and Related Functions", 76 | JOURNAL={Mathematics of Computation}, 77 | VOLUME={32}, 78 | YEAR={1978}, 79 | PAGES={1232--1240} } 80 | -------------------------------------------------------------------------------- /src/FindOpenCL.cmake: -------------------------------------------------------------------------------- 1 | # ######################################################################## 2 | # Copyright 2013 Advanced Micro Devices, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ######################################################################## 16 | 17 | 18 | # Locate an OpenCL implementation. 19 | # Currently supports AMD APP SDK (http://developer.amd.com/sdks/AMDAPPSDK/Pages/default.aspx/) 20 | # 21 | # Defines the following variables: 22 | # 23 | # OPENCL_FOUND - Found the OPENCL framework 24 | # OPENCL_INCLUDE_DIRS - Include directories 25 | # 26 | # Also defines the library variables below as normal 27 | # variables. These contain debug/optimized keywords when 28 | # a debugging library is found. 29 | # 30 | # OPENCL_LIBRARIES - libopencl 31 | # 32 | # Accepts the following variables as input: 33 | # 34 | # OPENCL_ROOT - (as a CMake or environment variable) 35 | # The root directory of the OpenCL implementation found 36 | # 37 | # FIND_LIBRARY_USE_LIB64_PATHS - Global property that controls whether findOpenCL should search for 38 | # 64bit or 32bit libs 39 | #----------------------- 40 | # Example Usage: 41 | # 42 | # find_package(OPENCL REQUIRED) 43 | # include_directories(${OPENCL_INCLUDE_DIRS}) 44 | # 45 | # add_executable(foo foo.cc) 46 | # target_link_libraries(foo ${OPENCL_LIBRARIES}) 47 | # 48 | #----------------------- 49 | 50 | find_path(OPENCL_INCLUDE_DIRS 51 | NAMES OpenCL/cl.h CL/cl.h 52 | HINTS 53 | ${OPENCL_ROOT}/include 54 | $ENV{AMDAPPSDKROOT}/include 55 | $ENV{CUDA_PATH}/include 56 | PATHS 57 | /usr/include 58 | /usr/local/include 59 | DOC "OpenCL header file path" 60 | ) 61 | mark_as_advanced( OPENCL_INCLUDE_DIRS ) 62 | 63 | # Search for 64bit libs if FIND_LIBRARY_USE_LIB64_PATHS is set to true in the global environment, 32bit libs else 64 | get_property( LIB64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS ) 65 | 66 | if( LIB64 ) 67 | find_library( OPENCL_LIBRARIES 68 | NAMES OpenCL 69 | HINTS 70 | ${OPENCL_ROOT}/lib 71 | $ENV{AMDAPPSDKROOT}/lib 72 | $ENV{CUDA_PATH}/lib 73 | DOC "OpenCL dynamic library path" 74 | PATH_SUFFIXES x86_64 x64 75 | PATHS 76 | /usr/lib 77 | ) 78 | else( ) 79 | find_library( OPENCL_LIBRARIES 80 | NAMES OpenCL 81 | HINTS 82 | ${OPENCL_ROOT}/lib 83 | $ENV{AMDAPPSDKROOT}/lib 84 | $ENV{CUDA_PATH}/lib 85 | DOC "OpenCL dynamic library path" 86 | PATH_SUFFIXES x86 Win32 87 | PATHS 88 | /usr/lib 89 | ) 90 | endif( ) 91 | mark_as_advanced( OPENCL_LIBRARIES ) 92 | 93 | include( FindPackageHandleStandardArgs ) 94 | FIND_PACKAGE_HANDLE_STANDARD_ARGS( OPENCL DEFAULT_MSG OPENCL_LIBRARIES OPENCL_INCLUDE_DIRS ) 95 | 96 | if( NOT OPENCL_FOUND ) 97 | message( STATUS "FindOpenCL looked for libraries named: OpenCL" ) 98 | endif() 99 | -------------------------------------------------------------------------------- /src/client/AsianOption/AsianOption.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #include 24 | #include "Types.h" 25 | #include "Simulate_Runs.h" 26 | 27 | int main() 28 | { 29 | int n = 1 << 15; // number of runs 30 | int n1 = 1 << 10; // number of workitems that will simulate n2 runs 31 | 32 | double r = 0.05; 33 | double sigma = 0.5; 34 | double strike = 100; 35 | double s0 = 100; 36 | int s = 12; 37 | 38 | // Array zeta[0..s] must contain zeta[0]=0.0, plus the s observation times. 39 | double* zeta = (double*)malloc((s + 1)*sizeof(double)); 40 | zeta[0] = 0.0; 41 | for (int j = 1; j <= s; j++) 42 | zeta[j] = (double)j / (double)s; 43 | 44 | //Set Device parameters 45 | cl_device_type device_type = CL_DEVICE_TYPE_CPU; 46 | int platform_index = 0; 47 | int device_index = 0; 48 | int ret = 0; 49 | 50 | //Start Simulation 51 | AsianOptionData data = { n, n1, r, sigma, strike, s0, s, zeta }; 52 | ret = call_with_opencl(platform_index, device_type, device_index, &SimulateAsianOption, &data, true); 53 | 54 | return ret; 55 | } 56 | -------------------------------------------------------------------------------- /src/client/AsianOption/AsianOptionKernel.cl: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #define CLPROBDIST_NORMAL_OBJ_MEM CLPROBDIST_MEM_TYPE_PRIVATE 24 | #define CLRNG_ENABLE_SUBSTREAMS 25 | 26 | /*! [clRNG header] */ 27 | #include 28 | /*! [clRNG header] */ 29 | 30 | #include "clProbDist/normal.clh" 31 | 32 | #pragma OPENCL EXTENSION cl_amd_printf : enable 33 | #pragma OPENCL EXTENSION cl_amd_fp64 : enable 34 | 35 | // Generates the process S. 36 | void generatePath(int s, __global double* logS, __global double* muDelta, __global double* sigmaSqrtDelta, 37 | clrngMrg32k3aStream* stream, clprobdistNormal* normalDist) { 38 | clprobdistStatus err; 39 | 40 | for (int j = 0; j < s; j++){ 41 | 42 | double u = clrngMrg32k3aRandomU01(stream); 43 | double d = clprobdistNormalInverseCDFWithObject(normalDist, u, &err); 44 | 45 | logS[j + 1] = logS[j] + muDelta[j] + sigmaSqrtDelta[j] * d; 46 | } 47 | 48 | } 49 | 50 | // Computes and returns the discounted option payoff. 51 | double getPayoff(int s, double strike, double discount, __global double* logS) { 52 | double average = 0.0; // Average of the GBM process. 53 | for (int j = 1; j <= s; j++) average += exp(logS[j]); 54 | average /= s; 55 | if (average > strike) return discount * (average - strike); 56 | else return 0.0; 57 | } 58 | 59 | //use distinct streams across the work items and n2 substreams within each work item. 60 | __kernel void AsianOptionSimulateGPU(__global clrngMrg32k3aHostStream* streams, __global clprobdistNormal* g_normalDist, __global double* stat_payOff, 61 | __global double* logS, __global double* muDelta, __global double* sigmaSqrtDelta) 62 | { 63 | // Each of the n work items executes the following code. 64 | int gid = get_global_id(0); // Id of this work item. 65 | int n1 = get_global_size(0); //Total number of work items 66 | 67 | // Make copies of the stream states in private memory. 68 | clrngMrg32k3aStream stream_d; 69 | clrngMrg32k3aCopyOverStreamsFromGlobal(1, &stream_d, &streams[gid]); 70 | 71 | //Make a copy of clprobdistNormal in private memory 72 | clprobdistNormal p_normalDist = *g_normalDist; 73 | 74 | //Simulate 75 | for (int i = 0; i < param_n2; i++) { 76 | 77 | generatePath(param_s, logS, muDelta, sigmaSqrtDelta, &stream_d, &p_normalDist); 78 | stat_payOff[i * n1 + gid] = getPayoff(param_s, param_strike, param_discount, logS); 79 | 80 | clrngMrg32k3aForwardToNextSubstreams(1, &stream_d); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/client/AsianOption/Simulate_Runs.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #ifndef SIMULATERUNS_H 24 | #define SIMULATERUNS_H 25 | 26 | 27 | #include "../common.h" 28 | int SimulateAsianOption(cl_context context, cl_device_id device, cl_command_queue queue, void* data_); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/client/AsianOption/Types.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #ifndef ASIANOPTION_TYPES_H 24 | #define ASIANOPTION_TYPES_H 25 | 26 | typedef struct AsianOptionData_ { 27 | int n; 28 | int n1; 29 | double r; 30 | double sigma; 31 | double strike; 32 | double s0; 33 | int s; 34 | double* zeta; 35 | } AsianOptionData; 36 | 37 | #endif -------------------------------------------------------------------------------- /src/client/DocsTutorial/example1.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Pierre L'Ecuyer (2015) 21 | * 22 | */ 23 | 24 | #include 25 | #include // for sqrt() 26 | #include 27 | 28 | // #define EFFICIENT 29 | 30 | void normalConfidenceInterval(cl_int n, cl_double* observations, cl_double level, cl_double* lower, cl_double* upper) 31 | { 32 | // compute sample average and variance 33 | cl_double sum = 0.0; 34 | cl_double sum_squares = 0.0; 35 | for (size_t i = 0; i < n; i++) { 36 | sum += observations[i]; 37 | sum_squares += observations[i] * observations[i]; 38 | } 39 | cl_double average = sum / n; 40 | cl_double variance = (sum_squares - average * sum) / (n - 1); 41 | 42 | #ifdef EFFICIENT 43 | // compute confidence interval 44 | cl_double half_width = sqrt(variance / n) * 45 | clprobdistStdNormalInverseCDF(0.5 * (1.0 + level), NULL); 46 | *lower = average - half_width; 47 | *upper = average + half_width; 48 | #else 49 | // compute normal distribution parameters 50 | cl_double mu = average; 51 | cl_double sigma = sqrt(variance / n); 52 | // compute confidence interval 53 | *lower = clprobdistNormalInverseCDF(mu, sigma, 0.5 * (1.0 - level), NULL); 54 | *upper = clprobdistNormalInverseCDF(mu, sigma, 0.5 * (1.0 + level), NULL); 55 | #endif 56 | } 57 | 58 | int main() 59 | { 60 | cl_double observations[] = { 61 | 0.39370309, 0.45559733, 0.02482335, 0.84412496, 0.162165, 0.16980586, 62 | 0.16837318, 0.21349251, 0.23347176, 0.67321979, 0.82547116, 0.47291367 63 | }; 64 | cl_double level = 0.95; 65 | cl_double lower, upper; 66 | normalConfidenceInterval(sizeof(observations) / sizeof(observations[0]), 67 | observations, level, &lower, &upper); 68 | printf("%g%% confidence interval: [%.2g, %.2g]\n", level * 100, lower, upper); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /src/client/DocsTutorial/example2.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Pierre L'Ecuyer (2015) 21 | * 22 | */ 23 | 24 | #include 25 | #include 26 | 27 | cl_double simulateOneRun(cl_double lambda, clrngMrg31k3pStream* stream) 28 | { 29 | cl_double output = 0.0; 30 | for (int i = 0; i < 100; i++) { 31 | cl_double u = clrngMrg31k3pRandomU01(stream); 32 | cl_int poisson_variate = clprobdistPoissonInverseCDF(lambda, u, NULL); 33 | // modify output using poisson_variate 34 | output += poisson_variate / (100.0 * clprobdistPoissonMean(lambda, NULL)); 35 | } 36 | return output; 37 | } 38 | 39 | int main() 40 | { 41 | // prepare 42 | int replications = 1024; 43 | cl_double lambda = 50.0; 44 | clrngMrg31k3pStream* stream = clrngMrg31k3pCreateStreams(NULL, 1, NULL, NULL); 45 | 46 | // simulate 47 | cl_double sum = 0.0; 48 | for (int i = 0; i < replications; i++) 49 | sum += simulateOneRun(lambda, stream); 50 | printf("The average output is %.3f\n", sum / replications); 51 | 52 | // clean up 53 | clrngMrg31k3pDestroyStreams(stream); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /src/client/DocsTutorial/example3.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Pierre L'Ecuyer (2015) 21 | * 22 | */ 23 | 24 | #include 25 | #include 26 | 27 | cl_double simulateOneRun(const clprobdistPoisson* dist, clrngMrg31k3pStream* stream) 28 | { 29 | cl_double output = 0.0; 30 | for (int i = 0; i < 100; i++) { 31 | cl_double u = clrngMrg31k3pRandomU01(stream); 32 | cl_int poisson_variate = clprobdistPoissonInverseCDFWithObject(dist, u, NULL); 33 | // modify output using poisson_variate 34 | output += poisson_variate / (100.0 * clprobdistPoissonMeanWithObject(dist, NULL)); 35 | } 36 | return output; 37 | } 38 | 39 | int main() 40 | { 41 | // prepare 42 | int replications = 1024; 43 | clprobdistPoisson* dist = clprobdistPoissonCreate(50.0, NULL, NULL); 44 | clrngMrg31k3pStream* stream = clrngMrg31k3pCreateStreams(NULL, 1, NULL, NULL); 45 | 46 | // simulate 47 | cl_double sum = 0.0; 48 | for (int i = 0; i < replications; i++) 49 | sum += simulateOneRun(dist, stream); 50 | printf("The average output is %.3f\n", sum / replications); 51 | 52 | // clean up 53 | clprobdistPoissonDestroy(dist); 54 | clrngMrg31k3pDestroyStreams(stream); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /src/client/DocsTutorial/example4.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Pierre L'Ecuyer (2015) 21 | * 22 | */ 23 | 24 | #if defined(__APPLE__) || defined(__MACOSX) 25 | #include 26 | #else 27 | #include 28 | #endif 29 | 30 | #include 31 | #include 32 | #include "../common.h" 33 | 34 | #include 35 | #include 36 | 37 | int task(cl_context context, cl_device_id device, cl_command_queue queue, void* data) 38 | { 39 | cl_int err; 40 | size_t numWorkItems = *(size_t*)data; 41 | 42 | /*! [create distribution object] */ 43 | size_t dist_buf_size; 44 | clprobdistPoisson* dist = clprobdistPoissonCreate(50.0, &dist_buf_size, NULL); 45 | /*! [create distribution object] */ 46 | check_error(err, NULL); 47 | /*! [create streams] */ 48 | size_t streams_buf_size; 49 | clrngMrg31k3pStream* streams = clrngMrg31k3pCreateStreams(NULL, numWorkItems, 50 | &streams_buf_size, (clrngStatus *)&err); 51 | /*! [create streams] */ 52 | check_error(err, "cannot create random streams array"); 53 | 54 | cl_program program = build_program_from_file(context, device, 55 | "client/DocsTutorial/example4_kernel.cl", NULL, PATH_RELATIVE_TO_LIB, 56 | clrngGetLibraryDeviceIncludes(&err)); 57 | check_error(err, NULL); 58 | cl_kernel kernel = clCreateKernel(program, "my_kernel", &err); 59 | check_error(err, "cannot create kernel"); 60 | 61 | 62 | // Create buffer to transfer streams to the device. 63 | /*! [create streams buffer] */ 64 | cl_mem streams_buf = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, 65 | streams_buf_size, streams, &err); 66 | /*! [create streams buffer] */ 67 | check_error(err, "cannot create streams buffer"); 68 | /*! [create dist buffer] */ 69 | // Create buffer to transfer streams to the device. 70 | cl_mem dist_buf = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, 71 | dist_buf_size, dist, &err); 72 | /*! [create dist buffer] */ 73 | check_error(err, "cannot create dist buffer"); 74 | // Create buffer to transfer output back from the device. 75 | /*! [create output buffer] */ 76 | cl_mem out_buf = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, 77 | numWorkItems * sizeof(cl_double), NULL, &err); 78 | /*! [create output buffer] */ 79 | check_error(err, "cannot create output buffer"); 80 | 81 | // Set kernel arguments 82 | err = clSetKernelArg(kernel, 0, sizeof(dist_buf), &dist_buf); 83 | err |= clSetKernelArg(kernel, 1, sizeof(streams_buf), &streams_buf); 84 | err |= clSetKernelArg(kernel, 2, sizeof(out_buf), &out_buf); 85 | check_error(err, "cannot set kernel arguments"); 86 | 87 | // Enqueue the kernel on device. 88 | cl_event ev; 89 | err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &numWorkItems, NULL, 0, NULL, &ev); 90 | check_error(err, "cannot enqueue kernel"); 91 | 92 | // Wait for all work items to finish. 93 | err = clWaitForEvents(1, &ev); 94 | check_error(err, "error waiting for events"); 95 | 96 | cl_ulong t0, t1; 97 | clGetEventProfilingInfo(ev, CL_PROFILING_COMMAND_START, sizeof(t0), &t0, NULL); 98 | clGetEventProfilingInfo(ev, CL_PROFILING_COMMAND_END, sizeof(t1), &t1, NULL); 99 | cl_ulong total_time = t1 - t0; 100 | 101 | // Retrieve the contents of the output buffer from the device. 102 | cl_double* out = (cl_double*) malloc(numWorkItems * sizeof(cl_double)); 103 | err = clEnqueueReadBuffer(queue, out_buf, CL_TRUE, 0, 104 | numWorkItems * sizeof(out[0]), out, 0, NULL, NULL); 105 | check_error(err, "cannot read output buffer"); 106 | 107 | // printf("output buffer:\n"); 108 | // for (int j = 0; j < numWorkItems; j++) 109 | // printf(" %f\n", out[j]); 110 | 111 | double out_sum = 0.0; 112 | for (int j = 0; j < numWorkItems; j++) 113 | out_sum += out[j]; 114 | printf("\naverage output: %.3f\n", out_sum / numWorkItems); 115 | 116 | printf("\nprocessing time: %1.2f\n", total_time * 1e-9); 117 | 118 | clprobdistPoissonDestroy(dist); 119 | clrngMrg31k3pDestroyStreams(streams); 120 | free(out); 121 | clReleaseEvent(ev); 122 | clReleaseMemObject(dist_buf); 123 | clReleaseMemObject(streams_buf); 124 | clReleaseMemObject(out_buf); 125 | clReleaseKernel(kernel); 126 | clReleaseProgram(program); 127 | 128 | return EXIT_SUCCESS; 129 | } 130 | 131 | 132 | int main() 133 | { 134 | size_t numWorkItems = 1024; 135 | return call_with_opencl(0, CL_DEVICE_TYPE_CPU, 0, &task, &numWorkItems, true); 136 | } 137 | 138 | 139 | -------------------------------------------------------------------------------- /src/client/DocsTutorial/example4_kernel.cl: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Pierre L'Ecuyer (2015) 21 | * 22 | */ 23 | 24 | #include 25 | #include 26 | 27 | #ifndef NULL 28 | #define NULL ((void*)0) 29 | #endif 30 | 31 | double simulateOneRun(__global const clprobdistPoisson* dist, clrngMrg31k3pStream* stream) 32 | { 33 | double output = 0.0; 34 | for (int i = 0; i < 100; i++) { 35 | double u = clrngMrg31k3pRandomU01(stream); 36 | int poisson_variate = clprobdistPoissonInverseCDFWithObject(dist, u, NULL); 37 | // modify output using poisson_variate 38 | output += poisson_variate / (100.0 * clprobdistPoissonMeanWithObject(dist, NULL)); 39 | } 40 | return output; 41 | } 42 | 43 | __kernel void my_kernel( 44 | __global const clprobdistPoisson* dist, 45 | __global const clrngMrg31k3pHostStream* host_streams, 46 | __global double* output) 47 | { 48 | size_t gid = get_global_id(0); 49 | clrngMrg31k3pStream stream; 50 | clrngMrg31k3pCopyOverStreamsFromGlobal(1, &stream, &host_streams[gid]); 51 | output[gid] = simulateOneRun(dist, &stream); 52 | } 53 | -------------------------------------------------------------------------------- /src/client/Inventory/Policies.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #ifndef POLICIES_SS_H 24 | #define POLICIES_SS_H 25 | 26 | #include "Types.h" 27 | #include "../common.h" 28 | 29 | typedef struct OnePolicyData_ { 30 | int n; 31 | int n1; 32 | int m; 33 | int s; 34 | int S; 35 | int selectedDist; 36 | double lambda; 37 | double mu; 38 | double sigma; 39 | ExecType execType; 40 | simResult* SimResults; 41 | } OnePolicyData; 42 | 43 | typedef struct SeveralPoliciesData_ { 44 | int n; 45 | int n1; 46 | int m; 47 | int* s; 48 | int* S; 49 | int P; 50 | int selectedDist; 51 | double lambda; 52 | double mu; 53 | double sigma; 54 | ExecOption optionType; 55 | simResult * SimResults; 56 | bool useCreator; 57 | } SeveralPoliciesData; 58 | 59 | int one_Policy(cl_context context, cl_device_id device, cl_command_queue queue, void* data_); 60 | int several_Policies(cl_context context, cl_device_id device, cl_command_queue queue, void* data_); 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /src/client/Inventory/SimulateRuns.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #ifndef SIMULATERUNS_SS_H 24 | #define SIMULATERUNS_SS_H 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include "../common.h" 31 | 32 | //Device Simulation 33 | clrngStatus inventorySimulateRunsGPU(cl_context context, cl_device_id device, cl_command_queue queue, 34 | int m, int* s, int* S, int P, int n, int n1, int n2, ExecOption Option, 35 | const char * kernelName, 36 | size_t streamsBufSize, clrngMrg32k3aStream * streams_demand, clrngMrg32k3aStream * streams_order, 37 | int selectedDist, double* lambdaArr, 38 | size_t poissonBufSize, clprobdistPoisson* poissonDist, size_t normalBufSize, clprobdistNormal* normalDist, 39 | double *stat, simResult * results); 40 | 41 | //CPU Simulation 42 | clrngStatus inventorySimulateRunsCPU(int m, int s, int S, int n, 43 | double* lambdaArr,clrngMrg32k3aStream* streams_demand, clrngMrg32k3aStream* streams_order, 44 | int selectedDist, clprobdistPoisson* poissonDist, clprobdistNormal* normalDist, 45 | double *stat_profit, ExecType execType, simResult * results); 46 | 47 | 48 | 49 | #endif -------------------------------------------------------------------------------- /src/client/Inventory/Types.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #ifndef INVENTORY_TYPES_SS_H 24 | #define INVENTORY_TYPES_SS_H 25 | 26 | typedef enum ExecType_ { 27 | basic = 1, 28 | Case_a = 2, //Simulate n runs on n workitems using two streams and their substreams 29 | Case_b = 3, //Simulate n runs on n workitems using n distinct streams 30 | Case_c = 4, //Simulate n2 runs on n1 workitmes using 2n1 streams and their substreams 31 | Case_d = 5 //Simulate n2 runs on n1 workitems using 2n2 streams 32 | } ExecType; 33 | 34 | typedef enum ExecOption_ { 35 | OnePolicy = 0, 36 | Option1 = 1, // Simulate n = n1.n2 runs for p policies, with n1 work items and n2 runs per work item 37 | Option2 = 2 // Simulate n = n1.n2 runs for p policies, with n1p work items and n2 runs per work item, 38 | } ExecOption; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/client/Inventory/inventory.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | #include "../common.h" 28 | #include "Types.h" 29 | #include "Policies.h" 30 | #include 31 | #include "../../include/clProbDist/poisson.h" 32 | #include "../../include/clProbDist/gamma.h" 33 | 34 | 35 | int main() 36 | { 37 | 38 | //double alpha = 9; 39 | //double lambda = 0.5; 40 | //int decprec = 15; 41 | //clprobdistStatus err; 42 | 43 | //clprobdistGamma* gammaDist = clprobdistGammaCreate(alpha, lambda, decprec, &err); 44 | //clrngMrg32k3aStream* stream = clrngMrg32k3aCreateStreams(NULL, 1, NULL, NULL); 45 | 46 | //double r; 47 | 48 | //for (unsigned i = 0; i < 10; i++){ 49 | // double u = clrngMrg32k3aRandomU01(stream); 50 | // clprobdistGammaInverseCDF(u, gammaDist, &r); 51 | 52 | // printf("u %f r %f \n", u, r); 53 | //} 54 | 55 | 56 | /*size_t distBufSize; 57 | clprobdistStatus err; 58 | double lambda = 520; 59 | clprobdistPoisson* dist = clprobdistPoissonCreate(lambda, &distBufSize, &err); 60 | 61 | 62 | //Generate Poisson Random Numbers 63 | clrngMrg32k3aStream* stream = clrngMrg32k3aCreateStreams(NULL, 1, NULL, NULL); 64 | double u; 65 | int result; 66 | for (unsigned i = 0; i < 10; i++){ 67 | u = clrngMrg32k3aRandomU01(stream); 68 | clprobdistPoissonInverseCDFWithObject(dist, u, &result); 69 | 70 | printf("u %20.19f r %d \n", u, result); 71 | }*/ 72 | 73 | int selectedDist = 0, selectedSim = 0; 74 | 75 | printf(" Please select the distribution to use for generating the inventory's demand : \n\n"); 76 | printf(" (1) - Poisson dist. with fixed Lambda. \n"); 77 | printf(" (2) - Poisson dist. with random Lambda that follow a Gamma dist. for each day.\n"); 78 | printf(" (3) - Normal dist. truncated at 0\n"); 79 | printf("\n Choice (1 to 3) : "); 80 | scanf(" %d", &selectedDist); getchar(); 81 | //selectedDist = 3; 82 | 83 | printf("\n\n Please select the simulation to run : \n\n"); 84 | printf(" One Policy : \n"); 85 | printf(" (1) - n runs on CPU, using one stream \n"); 86 | printf(" (2) - n runs with n work items, using two streams and their substreams \n"); 87 | printf(" (3) - n runs with n work items, using two arrays of n stream each \n"); 88 | printf(" (4) - n2 runs with n1 work itmes, using 2*n1 streams their substreams \n"); 89 | printf(" (5) - n2 runs with n1 work items, using 2*n2 distinct streams. \n"); 90 | printf("\n Several policies : \n"); 91 | printf(" (6) - p policies in series, with n1 work items and n2 runs per work item\n"); /*option1*/ 92 | printf(" (7) - p policies in parallel, with n1p work items and n2 runs per work item \n"); /*option2*/ 93 | 94 | printf("\n Choice (0 to quit) : "); 95 | scanf(" %d", &selectedSim); getchar(); 96 | //selectedSim = 2; 97 | 98 | int m = 30; // number of days 99 | int n = 1 << 10; // number of runs 100 | int n1 = 1 << 8; // number of workitems that will simulate n2 runs 101 | 102 | double lambda = 50; 103 | double mu = 100; 104 | double sigma = 10; 105 | 106 | //Policies values 107 | int s[] = { 80, 80 }; 108 | int S[] = { 200, 198 }; 109 | int P = 2; // number of policies 110 | 111 | cl_device_type device_type = CL_DEVICE_TYPE_CPU; 112 | int platform_index = 0; 113 | int device_index = 0; 114 | int ret = 0; 115 | 116 | //Simulate different options : 117 | while (selectedSim != 0) 118 | { 119 | switch (selectedSim) 120 | { 121 | case 1: { 122 | printf("\n===========================================================================\n ONE POLICY:\n=============\n"); 123 | printf("+++++++++ On CPU (basic case): One policy, one stream \n"); 124 | OnePolicyData data = { n, 0, m, s[0], S[0], selectedDist, lambda, mu, sigma, basic }; 125 | ret = call_with_opencl(platform_index, device_type, device_index, &one_Policy, &data, true); 126 | 127 | break; 128 | } 129 | case 2: { 130 | printf("\n===========================================================================\n ONE POLICY:\n=============\n"); 131 | printf("+++++++++ On CPU (case a) : One policy, two streams with their substreams \n"); 132 | OnePolicyData data = { n, 0, m, s[0], S[0], selectedDist, lambda, mu, sigma, Case_a }; 133 | ret = call_with_opencl(platform_index, device_type, device_index, &one_Policy, &data, true); 134 | break; 135 | } 136 | case 3: { 137 | printf("\n===========================================================================\n ONE POLICY:\n=============\n"); 138 | printf("+++++++++ On CPU (case b): One policy, two arrays of n streams each \n"); 139 | OnePolicyData data = { n, 0, m, s[0], S[0], selectedDist, lambda, mu, sigma, Case_b }; 140 | ret = call_with_opencl(platform_index, device_type, device_index, &one_Policy, &data, true); 141 | break; 142 | } 143 | case 4: { 144 | printf("\n===========================================================================\n ONE POLICY:\n=============\n"); 145 | //no CPU implementation : (case c) 146 | OnePolicyData data = { n, n1, m, s[0], S[0], selectedDist, lambda, mu, sigma, Case_c }; 147 | ret = call_with_opencl(platform_index, device_type, device_index, &one_Policy, &data, true); 148 | break; 149 | } 150 | case 5: { 151 | printf("\n===========================================================================\n ONE POLICY:\n=============\n"); 152 | //no CPU implementation : (case d) 153 | OnePolicyData data = { n, n1, m, s[0], S[0], selectedDist, lambda, mu, sigma, Case_d }; 154 | ret = call_with_opencl(platform_index, device_type, device_index, &one_Policy, &data, true); 155 | break; 156 | } 157 | case 6: { 158 | printf("\n===========================================================================\n Several policies Option1:\n=========\n"); 159 | //no CPU implementation : Several policies (option 1) 160 | SeveralPoliciesData data = { n, n1, m, s, S, P, selectedDist, lambda, mu, sigma, Option1 }; 161 | ret = call_with_opencl(platform_index, device_type, device_index, &several_Policies, &data, true); 162 | break; 163 | } 164 | case 7: { 165 | printf("\n===========================================================================\n Several policies Option2:\n=========\n"); 166 | //no CPU implementation : Several policies (option 2) 167 | SeveralPoliciesData data = { n, n1, m, s, S, P, selectedDist, lambda, mu, sigma, Option2 }; 168 | ret = call_with_opencl(platform_index, device_type, device_index, &several_Policies, &data, false); 169 | break; 170 | } 171 | 172 | default: 173 | break; 174 | } 175 | 176 | printf("\n Choice (0 to quit) : "); 177 | scanf(" %d", &selectedSim); 178 | } 179 | 180 | 181 | return ret; 182 | } 183 | -------------------------------------------------------------------------------- /src/client/Inventory/test.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | int main() 27 | { 28 | cl_double lambda = 50.0; 29 | clprobdistPoisson* dist = clprobdistPoissonCreate(50.0, NULL, NULL); 30 | clrngMrg31k3pStream* stream = clrngMrg31k3pCreateStreams(NULL, 1, NULL, NULL); 31 | 32 | for (int i = 0; i < 30; i++) { 33 | 34 | cl_double u = clrngMrg31k3pRandomU01(stream); 35 | 36 | cl_int with_param = clprobdistPoissonInverseCDF(lambda, u, NULL); 37 | 38 | cl_int with_object = clprobdistPoissonInverseCDFWithObject(dist, u, NULL); 39 | 40 | printf("u=%f, with param/obj=%d/%d %s\n", u, with_param, with_object, 41 | with_param == with_object ? "" : "<--"); 42 | } 43 | 44 | 45 | clprobdistPoissonDestroy(dist); 46 | clrngMrg31k3pDestroyStreams(stream); 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /src/client/Performance/Options.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include "../common.h" 29 | #include "Options.h" 30 | #include "SimulateRun.h" 31 | 32 | /*! [clRNG header] */ 33 | #include 34 | /*! [clRNG header] */ 35 | 36 | #include "../../include/clProbDist/gamma.h" 37 | #include "../../include/clProbDist/normal.h" 38 | 39 | int Option1(cl_context context, cl_device_id device, cl_command_queue queue, void* data_) 40 | { 41 | OptionData* data = (OptionData*)data_; 42 | int n = data->n; 43 | int n1 = data->n1; 44 | double * lambdaArr = data->lambdaArr; 45 | 46 | //Declare vars 47 | clrngStatus err; 48 | size_t streamBufferSize; 49 | 50 | //Create stream demand 51 | clrngMrg32k3aStream* streams = clrngMrg32k3aCreateStreams(NULL, n1, &streamBufferSize, &err); 52 | check_error(err, "%s(): cannot create random streams demand", __func__); 53 | 54 | printf("\nSimulate Poisson Distribution with Object :\n"); 55 | printf("On Device :\n"); 56 | for (int i = 0; i < 3; i++) 57 | for (int j = 0; j < 3; j++) 58 | performanceOnGPU_Poisson(context, device, queue, "performancePoissonWithObject", n, n1, lambdaArr[i], streamBufferSize, streams, simWithObject); 59 | 60 | //************************************************************** 61 | printf("\nSimulate Poisson Distribution with static calls :\n"); 62 | printf("On Device :\n"); 63 | for (int i = 0; i < 3; i++) 64 | for (int j = 0; j < 3; j++) 65 | performanceOnGPU_Poisson(context, device, queue, "performancePoisson", n, n1, lambdaArr[i], streamBufferSize, streams, simStatic); 66 | 67 | //Free resources 68 | clrngMrg32k3aDestroyStreams(streams); 69 | 70 | return EXIT_SUCCESS; 71 | } 72 | 73 | int Option2(cl_context context, cl_device_id device, cl_command_queue queue, void* data_) 74 | { 75 | OptionData* data = (OptionData*)data_; 76 | int n = data->n; 77 | int n1 = data->n1; 78 | 79 | //Declare vars 80 | clrngStatus err; 81 | size_t streamBufferSize; 82 | 83 | //Create stream demand 84 | clrngMrg32k3aStream* streams = clrngMrg32k3aCreateStreams(NULL, n1, &streamBufferSize, &err); 85 | check_error(err, "%s(): cannot create random streams demand", __func__); 86 | 87 | //Set Std Noraml Distribution 88 | size_t normalBufSize = 0; 89 | clprobdistNormal* normalDist = clprobdistNormalCreate(0, 1, &normalBufSize, NULL); 90 | 91 | printf("\nSimulate Normal Distribution copied on private memory :\n"); 92 | for (int i = 0; i < 3; i++) 93 | performanceOnGPU_NoramlDist(context, device, queue, "performancePrivateNormalDist", n, n1, streamBufferSize, streams, normalBufSize, normalDist); 94 | 95 | //************************************************************** 96 | printf("\n\nSimulate Normal Distribution copied on global memory :\n"); 97 | printf("On Device :\n"); 98 | for (int i = 0; i < 3; i++) 99 | performanceOnGPU_NoramlDist(context, device, queue, "performanceGlobalNormalDist", n, n1, streamBufferSize, streams, normalBufSize, normalDist); 100 | 101 | //Free resources 102 | clrngMrg32k3aDestroyStreams(streams); 103 | clprobdistNormalDestroy(normalDist); 104 | return EXIT_SUCCESS; 105 | } 106 | 107 | int Option3(cl_context context, cl_device_id device, cl_command_queue queue, void* data_) 108 | { 109 | OptionData* data = (OptionData*)data_; 110 | int n = data->n; 111 | int n1 = data->n1; 112 | double * lambdaArr = data->lambdaArr; 113 | 114 | //Declare vars 115 | clrngStatus err; 116 | size_t streamBufferSize; 117 | 118 | //Create stream demand 119 | clrngMrg32k3aStream* streams = clrngMrg32k3aCreateStreams(NULL, n1, &streamBufferSize, &err); 120 | check_error(err, "%s(): cannot create random streams demand", __func__); 121 | 122 | printf("\nSimulate Poisson Distribution with Object in local memory :\n"); 123 | for (int i = 0; i < 3; i++) 124 | performanceOnGPU_PoissonGlobalLocal(context, device, queue, "performancePoissonGlobalLocal", n, n1, lambdaArr[0], streamBufferSize, streams, simUseLocalMem); 125 | 126 | //************************************************************** 127 | printf("\nSimulate Poisson Distribution with Object in global memory :\n"); 128 | for (int i = 0; i < 3; i++) 129 | performanceOnGPU_PoissonGlobalLocal(context, device, queue, "performancePoissonGlobalLocal", n, n1, lambdaArr[0], streamBufferSize, streams, simUseGlobalMem); 130 | 131 | 132 | //Free resources 133 | clrngMrg32k3aDestroyStreams(streams); 134 | 135 | return EXIT_SUCCESS; 136 | } 137 | 138 | 139 | -------------------------------------------------------------------------------- /src/client/Performance/Options.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #ifndef POLICIES_SS_H 24 | #define POLICIES_SS_H 25 | 26 | #include "../common.h" 27 | 28 | typedef enum ExecType_ { 29 | simWithObject = 1, //Call function with Object 30 | simStatic = 2, //call static function of the distribution 31 | simUseGlobalMem = 3, 32 | simUseLocalMem = 4 33 | } ExecType; 34 | 35 | typedef struct OptionData_ { 36 | int n; 37 | int n1; 38 | double* lambdaArr; 39 | } OptionData; 40 | 41 | 42 | int Option1(cl_context context, cl_device_id device, cl_command_queue queue, void* data_); 43 | 44 | int Option2(cl_context context, cl_device_id device, cl_command_queue queue, void* data_); 45 | 46 | int Option3(cl_context context, cl_device_id device, cl_command_queue queue, void* data_); 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /src/client/Performance/PerformanceKernels.cl: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | /*! [clRNG header] */ 24 | #include 25 | /*! [clRNG header] */ 26 | 27 | #include "clProbDist/poisson.clh" 28 | #include "clProbDist/normal.clh" 29 | 30 | #pragma OPENCL EXTENSION cl_amd_printf : enable 31 | 32 | 33 | //****************************************************************************************** 34 | // Option 1 : 35 | //****************************************************************************************** 36 | //use distinct streams across the work items and n2 loop within each work item. 37 | __kernel void performancePoissonWithObject(__global clrngMrg32k3aHostStream* streams,_CLPROBDIST_POISSON_OBJ_MEM clprobdistPoisson* poissonDist) 38 | { 39 | clprobdistStatus err; 40 | 41 | // Each of the n work items executes the following code. 42 | int gid = get_global_id(0); // Id of this work item. 43 | int group_size = get_local_size(0); 44 | 45 | // Make copies of the stream states in private memory. 46 | clrngMrg32k3aStream stream_d; 47 | clrngMrg32k3aCopyOverStreamsFromGlobal(1, &stream_d, &streams[gid]); 48 | 49 | //Simulate n2 random nbr generation 50 | for (int i = 0; i < param_n2; i++) { 51 | clprobdistPoissonInverseCDFWithObject(poissonDist, clrngMrg32k3aRandomU01(&stream_d), &err); 52 | } 53 | } 54 | 55 | //use distinct streams across the work items and n2 loop within each work item. 56 | __kernel void performancePoisson(__global clrngMrg32k3aHostStream* streams) 57 | { 58 | clprobdistStatus err; 59 | 60 | // Each of the n work items executes the following code. 61 | int gid = get_global_id(0); // Id of this work item. 62 | 63 | // Make copies of the stream states in private memory. 64 | clrngMrg32k3aStream stream_d; 65 | clrngMrg32k3aCopyOverStreamsFromGlobal(1, &stream_d, &streams[gid]); 66 | 67 | //Simulate n2 random nbr generation 68 | for (int i = 0; i < param_n2; i++) { 69 | clprobdistPoissonInverseCDF(param_lambda, clrngMrg32k3aRandomU01(&stream_d), &err); 70 | } 71 | } 72 | 73 | //****************************************************************************************** 74 | // Option 2 : 75 | //****************************************************************************************** 76 | 77 | __kernel void performancePrivateNormalDist(__global clrngMrg32k3aHostStream* streams, __global clprobdistNormal* g_normalDist) 78 | { 79 | clprobdistStatus err; 80 | 81 | // Each of the n work items executes the following code. 82 | int gid = get_global_id(0); // Id of this work item. 83 | 84 | // Make copies of the stream states in private memory. 85 | clrngMrg32k3aStream stream_d; 86 | clrngMrg32k3aCopyOverStreamsFromGlobal(1, &stream_d, &streams[gid]); 87 | 88 | //Make a copy of clprobdistNormal in private memory 89 | clprobdistNormal p_normalDist = *g_normalDist; 90 | 91 | //Simulate n2 random nbr generation 92 | for (int i = 0; i < param_n2; i++) { 93 | clprobdistNormalInverseCDFWithObject(&p_normalDist, clrngMrg32k3aRandomU01(&stream_d), &err); 94 | } 95 | } 96 | __kernel void performanceGlobalNormalDist(__global clrngMrg32k3aHostStream* streams, __constant clprobdistNormal* c_normalDist) 97 | { 98 | clprobdistStatus err; 99 | 100 | // Each of the n work items executes the following code. 101 | int gid = get_global_id(0); // Id of this work item. 102 | 103 | // Make copies of the stream states in private memory. 104 | clrngMrg32k3aStream stream_d; 105 | clrngMrg32k3aCopyOverStreamsFromGlobal(1, &stream_d, &streams[gid]); 106 | 107 | //Simulate n2 random nbr generation 108 | for (int i = 0; i < param_n2; i++) { 109 | clprobdistNormalInverseCDFWithObject(&c_normalDist, clrngMrg32k3aRandomU01(&stream_d), &err); 110 | } 111 | } 112 | 113 | //****************************************************************************************** 114 | // Option 3 : 115 | //****************************************************************************************** 116 | 117 | __kernel void performancePoissonGlobalLocal(__global clrngMrg32k3aHostStream* streams, 118 | #if CLPROBDIST_POISSON_OBJ_MEM == CLPROBDIST_MEM_TYPE_LOCAL 119 | __global clprobdistPoisson* g_poissonDist, 120 | __local clprobdistPoisson* poissonDist 121 | #else 122 | _CLPROBDIST_POISSON_OBJ_MEM clprobdistPoisson* poissonDist 123 | #endif 124 | ) 125 | { 126 | clprobdistStatus err; 127 | 128 | // Each of the n work items executes the following code. 129 | int gid = get_global_id(0); // Id of this work item. 130 | int group_size = get_local_size(0); 131 | 132 | // Make copies of the stream states in private memory. 133 | clrngMrg32k3aStream stream_d; 134 | clrngMrg32k3aCopyOverStreamsFromGlobal(1, &stream_d, &streams[gid]); 135 | 136 | #if CLPROBDIST_POISSON_OBJ_MEM == CLPROBDIST_MEM_TYPE_LOCAL 137 | //First work-item of each group make a copy of global Poissont Distribution to local memory. 138 | if (gid % group_size == 0) 139 | clprobdistPoissonCopyOverFromGlobal(poissonDist, g_poissonDist); 140 | 141 | //All work-items sync here to make sure that the first work-item has finished copying. 142 | barrier(CLK_LOCAL_MEM_FENCE); 143 | #endif 144 | 145 | //Simulate n2 random nbr generation 146 | for (int i = 0; i < param_n2; i++) { 147 | clprobdistPoissonInverseCDFWithObject(poissonDist, clrngMrg32k3aRandomU01(&stream_d), &err); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/client/Performance/SimulateRun.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #ifndef SIMULATERUN_SS_H 24 | #define SIMULATERUN_SS_H 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include "../common.h" 31 | 32 | //Device Simulation 33 | void performanceOnGPU_Poisson(cl_context context, cl_device_id device, cl_command_queue queue, const char * kernelName, 34 | int n, int n1, double lambda, size_t streamsBufSize, clrngMrg32k3aStream* streams, ExecType execType); 35 | 36 | void performanceOnGPU_NoramlDist(cl_context context, cl_device_id device, cl_command_queue queue, const char * kernelName, 37 | int n, int n1, size_t streamsBufSize, clrngMrg32k3aStream* streams, size_t normalBufSize, clprobdistNormal* normalDist); 38 | 39 | void performanceOnGPU_PoissonGlobalLocal(cl_context context, cl_device_id device, cl_command_queue queue, const char * kernelName, 40 | int n, int n1, double lambda, size_t streamsBufSize, clrngMrg32k3aStream* streams, ExecType execType); 41 | 42 | //CPU Simulation 43 | void performanceOnCPU_Poisson(int n, double lambda, clrngMrg32k3aStream* stream, ExecType execType); 44 | 45 | #endif -------------------------------------------------------------------------------- /src/client/Performance/performance.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | #include 27 | #include "../common.h" 28 | #include "Options.h" 29 | #include 30 | #include "../../include/clProbDist/poisson.h" 31 | #include "../../include/clProbDist/gamma.h" 32 | 33 | #include "SimulateRun.h" 34 | 35 | 36 | int main() 37 | { 38 | int selectedSim = 0; 39 | 40 | printf(" Please select the option to execute : \n\n"); 41 | printf(" (1) Option1 : Performance of PoissonDist with different Lambda using objects and static calls \n"); 42 | printf(" (2) Option2 : Performance of NormalDist with dist on private memory vs contant memory \n"); 43 | printf(" (3) Option3 : Performance of PoissontDist where Dist Object is copied on local memory vs global memory \n"); 44 | 45 | printf("\n Choice (0 to quit) : "); 46 | scanf(" %d", &selectedSim); getchar(); 47 | 48 | cl_device_type device_type = CL_DEVICE_TYPE_CPU; 49 | int platform_index = 0; 50 | int device_index = 0; 51 | int ret = 0; 52 | 53 | int n = 1 << 25; // number of runs 54 | int n1 = 1 << 20; // number of workitems that will simulate n2 runs 55 | double lambdaArr[] = { 2.0, 10.0, 1000.0 }; 56 | 57 | //selectedSim = 1; 58 | 59 | //Simulate different options : 60 | while (selectedSim != 0) 61 | { 62 | switch (selectedSim) 63 | { 64 | case 1: { 65 | //Performance of PoissonDist with different Lambda using objects and static calls\n"); 66 | OptionData data = { n, n1, lambdaArr }; 67 | ret = call_with_opencl(platform_index, device_type, device_index, &Option1, &data, true); 68 | break; 69 | } 70 | 71 | case 2: { 72 | //Performance of NormalDist with dist on private memory vs contant memory\n"); 73 | OptionData data = { n, n1 }; 74 | ret = call_with_opencl(platform_index, device_type, device_index, &Option2, &data, true); 75 | break; 76 | } 77 | case 3: { 78 | //Performance of PoissontDist where Dist Object is copied on local memory vs global memory 79 | OptionData data = { n, n1, &lambdaArr[1] }; 80 | ret = call_with_opencl(platform_index, device_type, device_index, &Option3, &data, true); 81 | break; 82 | } 83 | default: 84 | break; 85 | } 86 | 87 | printf("\n Choice (0 to quit) : "); 88 | scanf(" %d", &selectedSim); 89 | } 90 | 91 | 92 | return ret; 93 | } 94 | -------------------------------------------------------------------------------- /src/client/common.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #ifndef COMMON_H 26 | #define COMMON_H 27 | 28 | #ifdef __APPLE__ 29 | #include 30 | #else 31 | #include 32 | #endif 33 | 34 | #include 35 | 36 | #if defined ( WIN32 ) 37 | #define __func__ __FUNCTION__ 38 | #endif 39 | 40 | /*! [clRNG header] */ 41 | #include 42 | /*! [clRNG header] */ 43 | 44 | #ifndef __cplusplus 45 | typedef enum b_ 46 | { 47 | false = 0, 48 | true =1 49 | } bool; 50 | #endif 51 | 52 | typedef enum simType_ { 53 | CPU_Exec = 1, 54 | GPU_Exec = 2 55 | } simType; 56 | 57 | typedef struct simResult_ { 58 | int ExecOption; // menu option executed = 1..7 59 | simType SimType; // CPU or GPU 60 | //Results 61 | double CPU_time; 62 | double average; 63 | double variance; 64 | double CI_low; 65 | double CI_high; 66 | 67 | } simResult; 68 | 69 | typedef struct policyPoint_{ 70 | int s, S; 71 | } policyPoint; 72 | 73 | //Confidence Interval calculation 74 | void computeCI(int n, double* statTally, simResult * results); 75 | 76 | /*! @brief Interrupt the program if an error has occurred. 77 | * 78 | * Print the error message \c msg to standard error and exists the program if 79 | * \c errcode < 0. 80 | * If \c msg is NULL, clrngGetErrorString() is invoked to obtain the message 81 | * string. 82 | */ 83 | void check_error(cl_int errcode, const char* msg, ...); 84 | 85 | /*! @brief Reads a whole file in a buffer. 86 | * 87 | * Necessary memory is allocated and must be released manually with free(). 88 | */ 89 | int read_file(const char* filename, char** pbuf); 90 | 91 | /*! @brief Retrieve the specified OpenCL device name. 92 | * 93 | * A pointer to a static memory location is returned. 94 | * It is overwritten at each call go get_device_name(). 95 | */ 96 | const char* get_device_name(cl_device_id device); 97 | 98 | /*! @brief Retrieve the specified OpenCL device version. 99 | * 100 | * A pointer to a static memory location is returned. 101 | * It is overwritten at each call go get_device_version(). 102 | */ 103 | const char* get_device_version(cl_device_id device); 104 | 105 | /*! @brief Retrieve the specified OpenCL platform name. 106 | * 107 | * A pointer to a static memory location is returned. 108 | * It is overwritten at each call go get_platform_name(). 109 | */ 110 | const char* get_platform_name(cl_platform_id platform); 111 | 112 | /*! @brief Retrieve the specified OpenCL platform version. 113 | * 114 | * A pointer to a static memory location is returned. 115 | * It is overwritten at each call go get_platform_version(). 116 | */ 117 | const char* get_platform_version(cl_platform_id platform); 118 | 119 | /*! @brief Return the maximum workgroup size on the given device. 120 | * 121 | * @note The program displays an error message and is interrupted upon error. 122 | */ 123 | size_t get_max_workgroup_size(cl_device_id device); 124 | 125 | /*! @brief Write the build log to \c file. 126 | * 127 | * @note The program displays an error message and is interrupted upon error. 128 | */ 129 | void write_build_log(FILE* file, cl_program program, cl_device_id device); 130 | 131 | /*! @brief Type of path for use with load_kernel_from_file() 132 | * 133 | */ 134 | typedef enum PathType_ { PATH_ABSOLUTE, PATH_RELATIVE_TO_LIB } PathType; 135 | 136 | /*! @brief Create and build an OpenCL probram from a source file. 137 | * @param[in] context OpenCL context. 138 | * @param[in] device OpenCL device ID. 139 | * @param[in] source_file Path to the source file, relative to the 140 | * library root specified by the environment 141 | * variable CLRNG_ROOT if \c 142 | * relative_to_lib_root is 143 | * \c PATH_RELATIVE_TO_LIB. 144 | * @param[in] preprocessors allow the user to add preprocessor to the code source before it get build. 145 | * @param[in] path_type If PATH_RELATIVE_TO_LIB, \c source_file is 146 | * considered to be a path relative to the 147 | * library root. 148 | * @param[in] extra_options Additional options to pass to the OpenCL C 149 | * compiler. 150 | * @return Created and built OpenCL kernel. 151 | * 152 | * @note The program displays an error message and is interrupted upon error. 153 | */ 154 | cl_program build_program_from_file( 155 | cl_context context, 156 | cl_device_id device, 157 | const char* source_file, 158 | const char* preprocessors, 159 | PathType path_type, 160 | const char* extra_options); 161 | 162 | /*! Prepare the OpenCL environment and run a given task. 163 | * 164 | * The task is specified as a callback function. 165 | * The OpenCL resources for the context, device and command queue are managed 166 | * by this function. The task callback is responsible for managing its 167 | * buffers and kernels. 168 | * The context, device and command queue that are passed to the task function 169 | * must not be released by the user; they are managed by call_with_opencl(). 170 | * 171 | * @param[in] platform_index The OpenCL platform with corresponding index is selected. 172 | * @param[in] task Callback function. 173 | * @param[in] device_type CL_DEVICE_TYPE_CPU or CL_DEVICE_TYPE_GPU. 174 | * @param[in] device_index If < 0, the task is run for all devices, otherwise, the 175 | * device with corresponding index is selected. 176 | * @param[in] data Extra data to pass as the last argument to the 177 | * callback (can be NULL). 178 | * @param[in] echoVersion Used to activate the display of information about Platform/Device versions. 179 | */ 180 | int call_with_opencl( 181 | int platform_index, 182 | cl_device_type device_type, 183 | int device_index, 184 | int (*task)(cl_context,cl_device_id,cl_command_queue,void*), 185 | void* data, 186 | bool echoVersion); 187 | #endif 188 | -------------------------------------------------------------------------------- /src/client/selfcontained.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include 23 | #include 24 | 25 | 26 | int main( void ) 27 | { 28 | cl_int err; 29 | cl_platform_id platform = 0; 30 | cl_device_id device = 0; 31 | cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 }; 32 | cl_context ctx = 0; 33 | cl_command_queue queue = 0; 34 | cl_program program = 0; 35 | cl_kernel kernel = 0; 36 | cl_event event = 0; 37 | cl_mem bufIn, bufOut; 38 | double *out; 39 | const char *includes; 40 | char buildLog[4096]; 41 | size_t numWorkItems = 64; 42 | size_t i; 43 | clprobdistExponential *dist = 0; 44 | size_t distBufferSize = 0; 45 | size_t kernelLines = 0; 46 | 47 | /* Sample kernel that calls clProbDist device-side interfaces to compute the 48 | inverse CDF of the exponential distribution at different quantiles */ 49 | const char *kernelSrc[] = { 50 | "#include \n", 51 | " \n", 52 | "__kernel void example(__global const clprobdistExponential *dist, \n", 53 | " __global double *out) \n", 54 | "{ \n", 55 | " int gid = get_global_id(0); \n", 56 | " int gsize = get_global_size(0); \n", 57 | " double quantile = (gid + 0.5) / gsize; \n", 58 | " out[gid] = clprobdistExponentialInverseCDFWithObject( \n", 59 | " dist, quantile, (void *)0); \n", 60 | "} \n", 61 | }; 62 | 63 | /* Setup OpenCL environment. */ 64 | err = clGetPlatformIDs( 1, &platform, NULL ); 65 | err = clGetDeviceIDs( platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL ); // FIXME 66 | err = clGetDeviceIDs( platform, CL_DEVICE_TYPE_CPU, 1, &device, NULL ); 67 | 68 | props[1] = (cl_context_properties)platform; 69 | ctx = clCreateContext( props, 1, &device, NULL, NULL, &err ); 70 | #ifdef CL_VERSION_2_0 71 | queue = clCreateCommandQueueWithProperties( ctx, device, (cl_queue_properties[]){0}, &err ); 72 | #else 73 | queue = clCreateCommandQueue( ctx, device, 0, &err ); 74 | #endif 75 | 76 | 77 | /* Make sure CLPROBDIST_ROOT is specified to get library path */ 78 | includes = clprobdistGetLibraryDeviceIncludes(&err); 79 | if(err != CL_SUCCESS) printf("\n%s\nSpecify environment variable CLPROBDIST_ROOT as described\n", clprobdistGetErrorString()); 80 | 81 | /* Create sample kernel */ 82 | kernelLines = sizeof(kernelSrc) / sizeof(kernelSrc[0]); 83 | program = clCreateProgramWithSource(ctx, kernelLines, kernelSrc, NULL, &err); 84 | err = clBuildProgram(program, 1, &device, includes, NULL, NULL); 85 | if(err != CL_SUCCESS) 86 | { 87 | printf("\nclBuildProgram has failed\n"); 88 | clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 4096, buildLog, NULL); 89 | printf("%s", buildLog); 90 | exit(1); 91 | } 92 | kernel = clCreateKernel(program, "example", &err); 93 | 94 | /* Create an exponential distribution with unit mean */ 95 | dist = clprobdistExponentialCreate(1.0, &distBufferSize, (clprobdistStatus *)&err); 96 | 97 | /* Create buffers for the kernel */ 98 | bufIn = clCreateBuffer(ctx, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, distBufferSize, dist, &err); 99 | bufOut = clCreateBuffer(ctx, CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, numWorkItems * sizeof(cl_double), NULL, &err); 100 | 101 | /* Setup the kernel */ 102 | err = clSetKernelArg(kernel, 0, sizeof(bufIn), &bufIn); 103 | err = clSetKernelArg(kernel, 1, sizeof(bufOut), &bufOut); 104 | 105 | /* Execute the kernel and read back results */ 106 | err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &numWorkItems, NULL, 0, NULL, &event); 107 | err = clWaitForEvents(1, &event); 108 | out = (double *)malloc(numWorkItems * sizeof(out[0])); 109 | err = clEnqueueReadBuffer(queue, bufOut, CL_TRUE, 0, numWorkItems * sizeof(out[0]), out, 0, NULL, NULL); 110 | 111 | /* Display results and check that the original quantile is recovered by evaluating the CDF */ 112 | for (i = 0; i < numWorkItems; i++) 113 | printf("quantile %.3f : %9.3e -> %.3f\n", (i + 0.5) / numWorkItems, out[i], clprobdistExponentialCDFWithObject(dist, out[i], &err)); 114 | 115 | /* Release allocated resources */ 116 | clReleaseEvent(event); 117 | free(out); 118 | clReleaseMemObject(bufIn); 119 | clReleaseMemObject(bufOut); 120 | 121 | clReleaseKernel(kernel); 122 | clReleaseProgram(program); 123 | 124 | clReleaseCommandQueue(queue); 125 | clReleaseContext(ctx); 126 | 127 | return 0; 128 | } 129 | -------------------------------------------------------------------------------- /src/include/clProbDist/clProbDist.clh: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | /*! @file clProbDist.clh 26 | * @brief Common definitions for the device-side API of clProbDist 27 | */ 28 | 29 | #pragma once 30 | #ifndef CLPROBDIST_CLH 31 | #define CLPROBDIST_CLH 32 | 33 | #ifndef __OPENCL_C_VERSION__ 34 | #error "clProbDist/clProbDist.clh can be included in device code only" 35 | #endif 36 | 37 | typedef double cl_double; 38 | typedef float cl_float; 39 | typedef int cl_int; 40 | typedef uint cl_uint; 41 | typedef long cl_long; 42 | typedef ulong cl_ulong; 43 | typedef bool cl_bool; 44 | #define CL_TRUE true 45 | #define CL_FALSE false 46 | 47 | 48 | #ifdef CLPROBDIST_SINGLE_PRECISION 49 | #error "CLPROBDIST_SINGLE_PRECISION option not yet implemented" 50 | #endif 51 | 52 | typedef enum clprobdistStatus_ { 53 | CLPROBDIST_SUCCESS = 0, 54 | CLPROBDIST_INVALID_VALUE = -1 55 | } clprobdistStatus; 56 | 57 | /*! @cond PRIVATE 58 | * @brief Convenience macro 59 | * 60 | * This macro does nothing. 61 | * It is defined for convenience when adapting host code for the device. 62 | */ 63 | #define clprobdistSetErrorString(err, ...) (err) 64 | /*! @endcond 65 | */ 66 | 67 | /*! @name Memory types 68 | * @{ 69 | */ 70 | /*! @brief Constant for private memory 71 | */ 72 | #define CLPROBDIST_MEM_TYPE_PRIVATE 100 73 | /*! @brief Constant for local memory 74 | */ 75 | #define CLPROBDIST_MEM_TYPE_LOCAL 200 76 | /*! @brief Constant for constant memory 77 | */ 78 | #define CLPROBDIST_MEM_TYPE_CONSTANT 300 79 | /*! @brief Constant for global memory 80 | */ 81 | #define CLPROBDIST_MEM_TYPE_GLOBAL 400 82 | /*! @} */ 83 | 84 | #endif 85 | 86 | /* 87 | vim: ft=c.doxygen sw=4 88 | */ 89 | -------------------------------------------------------------------------------- /src/include/clProbDist/clProbDist.version.h.in: -------------------------------------------------------------------------------- 1 | #define clprobdistVersionMajor @CLPROBDIST_VERSION_MAJOR@ 2 | #define clprobdistVersionMinor @CLPROBDIST_VERSION_MINOR@ 3 | #define clprobdistVersionPatch @CLPROBDIST_VERSION_PATCH@ 4 | -------------------------------------------------------------------------------- /src/include/clProbDist/continuous.clh: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #pragma once 26 | #ifndef CONTINUOUSDIST_CLH 27 | #define CONTINUOUSDIST_CLH 28 | 29 | 30 | #include "clProbDist.clh" 31 | 32 | typedef struct _ContinuousDist{ 33 | int decPrec;// = 15; //Decimal degits of precision 34 | // [supportA, supportB] is the support of the pdf(x) 35 | double supportA;// = DBL_MIN; //NEGATIVE INFINITY 36 | double supportB;// = DBL_MAX; //POSITIVE INFINITY 37 | } clprobdistContinuous; 38 | 39 | 40 | //// x infinity for some distributions 41 | //const double _removed_clprobdistXBIG = 100.0; 42 | //const double _removed_clprobdistXBIGM = 1000.0; 43 | // 44 | //// clprobdistEPSARRAY[j]: Epsilon required for j decimal degits of precision 45 | //const double _removed_clprobdistEPSARRAY[] = { 46 | // 0.5, 0.5E-1, 0.5E-2, 0.5E-3, 0.5E-4, 0.5E-5, 0.5E-6, 0.5E-7, 0.5E-8, 47 | // 0.5E-9, 0.5E-10, 0.5E-11, 0.5E-12, 0.5E-13, 0.5E-14, 0.5E-15, 0.5E-16, 48 | // 0.5E-17, 0.5E-18, 0.5E-19, 0.5E-20, 0.5E-21, 0.5E-22, 0.5E-23, 0.5E-24, 49 | // 0.5E-25, 0.5E-26, 0.5E-27, 0.5E-28, 0.5E-29, 0.5E-30, 0.5E-31, 0.5E-32, 50 | // 0.5E-33, 0.5E-34, 0.5E-35 51 | //}; 52 | //// code that is common to host and device 53 | //#include 54 | 55 | 56 | 57 | #endif -------------------------------------------------------------------------------- /src/include/clProbDist/continuous.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #pragma once 26 | #ifndef CLPROBDIST_CONTINUOUSDIST_H 27 | #define CLPROBDIST_CONTINUOUSDIST_H 28 | 29 | #if defined(__APPLE__) || defined(__MACOSX) 30 | #include 31 | #else 32 | #include 33 | #endif 34 | 35 | /*! @cond PRIVATE */ 36 | 37 | /*! @file continuous.h 38 | * @brief Common definitions for continuous distributions 39 | * 40 | * Implementation of continuous distributions should include this header 41 | * 42 | * This file provides default implementations for \f$\bar F(x)\f$ 43 | * and for \f$F^{-1}(u)\f$, the latter using the Brent-Dekker method (see 44 | * \cite iBRE71a and \cite iBRE73a) to find the inverse of a generic distribution 45 | * function \f$F(f)\f$. 46 | * This is a robust root-finding method, appropriate to find a root \f$x\f$ of 47 | * \f$F(x)=u\f$ when \f$u\f$ is known and if we have already a method to compute 48 | * \f$F\f$. 49 | * This is a special case. 50 | * 51 | */ 52 | typedef struct _clprobdistContinuousDist{ 53 | int decPrec; //Decimal degits of precision 54 | // [supportA, supportB] is the support of the pdf(x) 55 | cl_double supportA; //NEGATIVE INFINITY 56 | cl_double supportB; //POSITIVE INFINITY 57 | } clprobdistContinuous; 58 | 59 | // None of these functions are currently used by the library, but they could be 60 | // used in the future when more distributions are implemented. 61 | 62 | #if 0 63 | /*********************************** 64 | * Constructor & destructor 65 | ***********************************/ 66 | cl_int clprobdistContinuousGetDecPrec(clprobdistContinuous* distObj, clprobdistStatus* err); 67 | 68 | cl_double clprobdistContinuousGetXinf(clprobdistContinuous* distObj, clprobdistStatus* err); 69 | 70 | cl_double clprobdistContinuousGetXsup(clprobdistContinuous* distObj, clprobdistStatus* err); 71 | 72 | clprobdistStatus clprobdistContinuousSetXinf(cl_double xa, clprobdistContinuous* distObj); 73 | 74 | clprobdistStatus clprobdistContinuousSetXsup(cl_double xb, clprobdistContinuous* distObj); 75 | 76 | 77 | 78 | /*********************************** 79 | * Abstract functions 80 | ***********************************/ 81 | cl_double clprobdistContinuousCDF(cl_double x, clprobdistStatus* err); 82 | 83 | clprobdistStatus findInterval(cl_double u, cl_double* iv, clprobdistContinuous* distObj, clprobdistStatus* err); 84 | 85 | cl_double clprobdistContinuousDensity(cl_double x, clprobdistStatus* err); 86 | 87 | cl_double clprobdistContinuousGetMean(clprobdistStatus* err); 88 | 89 | cl_double clprobdistContinuousGetVariance(clprobdistStatus* err); 90 | 91 | cl_double clprobdistContinuousGetStdDeviation(clprobdistStatus* err); 92 | 93 | /*********************************** 94 | * Default implementations of functions 95 | ***********************************/ 96 | cl_double clprobdistContinuousComplCDF(cl_double x, clprobdistStatus* err); 97 | 98 | cl_double clprobdistContinuousInverseBrent(cl_double a, cl_double b, cl_double u, cl_double tol, clprobdistContinuous* distObj, clprobdistStatus* err); 99 | 100 | cl_double clprobdistContinuousInverseBisection(cl_double u, clprobdistContinuous* distObj, clprobdistStatus* err); 101 | 102 | cl_double clprobdistContinuousInverseCDF(cl_double u, clprobdistContinuous* distObj, clprobdistStatus* err); 103 | #endif 104 | 105 | #endif /* CONTINUOUSDIST_H */ 106 | /*! @endcond */ 107 | -------------------------------------------------------------------------------- /src/include/clProbDist/exponential.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #pragma once 26 | #ifndef CLPROBDIST_EXPONENTIALDIST_H 27 | #define CLPROBDIST_EXPONENTIALDIST_H 28 | 29 | /*! @file exponential.h 30 | * @brief API of the exponential distribution 31 | * 32 | * Implementation of \ref clProbDist_template.h for the exponential distribution, 33 | * adapted from \cite iLEC08j . 34 | */ 35 | 36 | #include "clProbDist/clProbDist.h" 37 | #include "clProbDist/continuous.h" 38 | 39 | /*! @brief Exponential distribution object [**device**] 40 | * 41 | * A structure that represents an exponential distribution object. 42 | */ 43 | typedef struct _clprobdistExponential clprobdistExponential; 44 | 45 | 46 | /*! @name Functions to create and destroy distribution objects 47 | * 48 | * @{ 49 | */ 50 | 51 | /*! @brief @copybrief clprobdistCreate() 52 | * 53 | * Create a new exponential distribution object. 54 | * Since this function allocates memory for the new distribution object; 55 | * clprobdistDestroy() must be called to release the allocated memory. 56 | * 57 | * @param[in] lambda Value of the inverse mean \f$\lambda\f$. 58 | * @param[out] bufSize Size in bytes of the created distribution object, or \c NULL. 59 | * @param[out] err Error status variable, or \c NULL. 60 | * @return New distribution object. 61 | */ 62 | clprobdistExponential* clprobdistExponentialCreate(cl_double lambda, size_t* bufSize, clprobdistStatus* err); 63 | 64 | /*! @copydoc clprobdistDestroy() 65 | */ 66 | clprobdistStatus clprobdistExponentialDestroy(clprobdistExponential* dist); 67 | 68 | /*! @} */ 69 | 70 | 71 | /*! @name Functions for use with a distribution object 72 | * 73 | * @{ 74 | */ 75 | 76 | /*! @copydoc clprobdistDensityWithObject() 77 | */ 78 | cl_double clprobdistExponentialDensityWithObject(const clprobdistExponential* dist, cl_double x, clprobdistStatus* err); 79 | 80 | /*! @copydoc clprobdistCDFWithObject() 81 | */ 82 | cl_double clprobdistExponentialCDFWithObject(const clprobdistExponential* dist, cl_double x, clprobdistStatus* err); 83 | 84 | /*! @copydoc clprobdistComplCDFWithObject() 85 | */ 86 | cl_double clprobdistExponentialComplCDFWithObject(const clprobdistExponential* dist, cl_double x, clprobdistStatus* err); 87 | 88 | /*! @copydoc clprobdistInverseCDFWithObject() 89 | */ 90 | cl_double clprobdistExponentialInverseCDFWithObject(const clprobdistExponential* dist, cl_double u, clprobdistStatus* err); 91 | 92 | 93 | /*! @copydoc clprobdistMeanWithObject() 94 | */ 95 | cl_double clprobdistExponentialMeanWithObject(const clprobdistExponential* dist, clprobdistStatus* err); 96 | 97 | /*! @copydoc clprobdistVarianceWithObject() 98 | */ 99 | cl_double clprobdistExponentialVarianceWithObject(const clprobdistExponential* dist, clprobdistStatus* err); 100 | 101 | /*! @copydoc clprobdistStdDeviationWithObject() 102 | */ 103 | cl_double clprobdistExponentialStdDeviationWithObject(const clprobdistExponential* dist, clprobdistStatus* err); 104 | 105 | /*! @brief Return the value of the inverse mean \f$\lambda\f$ [**device**] 106 | */ 107 | cl_double clprobdistExponentialGetLambda(const clprobdistExponential* dist, clprobdistStatus* err); 108 | 109 | /*! @brief Change the value of the inverse mean \f$\lambda\f$ [**device**] 110 | */ 111 | clprobdistStatus clprobdistExponentialSetLambda(clprobdistExponential* dist, cl_double newlambda); 112 | 113 | /*! @} */ 114 | 115 | 116 | /*! @name Functions for use with explicit distribution parameters 117 | * 118 | * @{ 119 | */ 120 | 121 | /*! @brief @copybrief clprobdistDensity() 122 | * 123 | * @see clprobdistExponentialDensityWithObject() 124 | * 125 | * @param[in] lambda Value of the inverse mean \f$\lambda\f$. 126 | * @param[in] x Value of \f$x\f$. 127 | * @param[out] err Error status variable, or \c NULL. 128 | * @return Value of \f$f(x)\f$. 129 | */ 130 | cl_double clprobdistExponentialDensity(cl_double lambda, cl_double x, clprobdistStatus* err); 131 | 132 | /*! @brief @copybrief clprobdistCDF() 133 | * 134 | * @see clprobdistExponentialCDFWithObject() 135 | * 136 | * @param[in] lambda Value of the inverse mean \f$\lambda\f$. 137 | * @param[in] x Value of \f$x\f$. 138 | * @param[out] err Error status variable, or \c NULL. 139 | * @return Value of \f$F(x)\f$. 140 | */ 141 | cl_double clprobdistExponentialCDF(cl_double lambda, cl_double x, clprobdistStatus* err); 142 | 143 | /*! @brief @copybrief clprobdistComplCDF() 144 | * 145 | * @see clprobdistExponentialComplCDFWithObject() 146 | * 147 | * @param[in] lambda Value of the inverse mean \f$\lambda\f$. 148 | * @param[in] x Value of \f$x\f$. 149 | * @param[out] err Error status variable, or \c NULL. 150 | * @return Value of \f$\bar F(x)\f$. 151 | */ 152 | cl_double clprobdistExponentialComplCDF(cl_double lambda, cl_double x, clprobdistStatus* err); 153 | 154 | /*! @brief @copybrief clprobdistInverseCDF() 155 | * 156 | * @see clprobdistExponentialInverseCDFWithObject() 157 | * 158 | * @param[in] lambda Value of the inverse mean \f$\lambda\f$. 159 | * @param[in] u Value of \f$u \in [0,1]\f$. 160 | * @param[out] err Error status variable, or \c NULL. 161 | * @return Value of \f$F^{-1}(u)\f$. 162 | */ 163 | cl_double clprobdistExponentialInverseCDF(cl_double lambda, cl_double u, clprobdistStatus* err); 164 | 165 | /*! @brief @copybrief clprobdistMean() 166 | * 167 | * @see clprobdistExponentialMeanWithObject() 168 | * 169 | * @param[in] lambda Value of the inverse mean \f$\lambda\f$. 170 | * @param[out] err Error status variable, or \c NULL. 171 | * @return Mean of the distribution. 172 | */ 173 | cl_double clprobdistExponentialMean(cl_double lambda, clprobdistStatus* err); 174 | 175 | /*! @brief @copybrief clprobdistVariance() 176 | * 177 | * @see clprobdistExponentialVarianceWithObject() 178 | * 179 | * @param[in] lambda Value of the inverse mean \f$\lambda\f$. 180 | * @param[out] err Error status variable, or \c NULL. 181 | * @return Variance of the distribution. 182 | */ 183 | cl_double clprobdistExponentialVariance(cl_double lambda, clprobdistStatus* err); 184 | 185 | /*! @brief @copybrief clprobdistStdDeviation() 186 | * 187 | * @see clprobdistExponentialStdDeviationWithObject() 188 | * 189 | * @param[in] lambda Value of the inverse mean \f$\lambda\f$. 190 | * @param[out] err Error status variable, or \c NULL. 191 | * @return Standard deviation of the distribution. 192 | */ 193 | cl_double clprobdistExponentialStdDeviation(cl_double lambda, clprobdistStatus* err); 194 | 195 | /*! @} */ 196 | 197 | 198 | #endif 199 | -------------------------------------------------------------------------------- /src/include/clProbDist/poisson.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #pragma once 26 | #ifndef CLPROBDIST_POISSONDIST_H 27 | #define CLPROBDIST_POISSONDIST_H 28 | 29 | /*! @file poisson.h 30 | * @brief API of the Poisson distribution 31 | * 32 | * Implementation of \ref clProbDist_template.h for the Poisson distribution, 33 | * adapted from \cite iLEC08j . 34 | */ 35 | 36 | #include "clProbDist/clProbDist.h" 37 | #include 38 | 39 | 40 | /*! @brief Poisson distribution object [**device**] 41 | * 42 | * A structure that represents a Poisson distribution object. 43 | */ 44 | typedef struct _clprobdistPoisson clprobdistPoisson; 45 | 46 | /*********************************** 47 | * Constructor & destructor 48 | ***********************************/ 49 | 50 | 51 | /*! @name Functions to create and destroy distribution objects 52 | * 53 | * @{ 54 | */ 55 | 56 | /*! @brief @copybrief clprobdistCreate() 57 | * 58 | * Create a new Poisson distribution object. 59 | * Since this function allocates memory for the new distribution object; 60 | * clprobdistDestroy() must be called to release the allocated memory. 61 | * 62 | * @param[in] lambda Value of the mean \f$\lambda\f$. 63 | * @param[out] bufSize Size in bytes of the created distribution object, or \c NULL. 64 | * @param[out] err Error status variable, or \c NULL. 65 | * @return New distribution object. 66 | */ 67 | clprobdistPoisson* clprobdistPoissonCreate(cl_double lambda, size_t* bufSize, clprobdistStatus* err); 68 | 69 | /*! @copydoc clprobdistDestroy() 70 | */ 71 | clprobdistStatus clprobdistPoissonDestroy(clprobdistPoisson* dist); 72 | 73 | /*! @} */ 74 | 75 | /*********************************** 76 | * Implementation 77 | ***********************************/ 78 | 79 | 80 | //Dynamic Functions 81 | 82 | /*! @name Functions for use with a distribution object 83 | * 84 | * @{ 85 | */ 86 | 87 | /*! @copydoc clprobdistProbWithObject() 88 | */ 89 | cl_double clprobdistPoissonProbWithObject(const clprobdistPoisson* dist, cl_int x, clprobdistStatus* err); 90 | 91 | /*! @copydoc clprobdistCDFWithObject() 92 | */ 93 | cl_double clprobdistPoissonCDFWithObject(const clprobdistPoisson* dist, cl_int x, clprobdistStatus* err); 94 | 95 | /*! @copydoc clprobdistComplCDFWithObject() 96 | * 97 | * @warning The complementary distribution function is defined as 98 | * \f$\bar F(j) = \mathbb P[X \geq j]\f$. 99 | */ 100 | cl_double clprobdistPoissonComplCDFWithObject(const clprobdistPoisson* dist, cl_int x, clprobdistStatus* err); 101 | 102 | /*! @copydoc clprobdistInverseCDFWithObject() 103 | */ 104 | cl_int clprobdistPoissonInverseCDFWithObject(const clprobdistPoisson* dist, cl_double u, clprobdistStatus* err); 105 | 106 | /*! @copydoc clprobdistMeanWithObject() 107 | */ 108 | cl_double clprobdistPoissonMeanWithObject(const clprobdistPoisson* dist, clprobdistStatus* err); 109 | 110 | /*! @copydoc clprobdistVarianceWithObject() 111 | */ 112 | cl_double clprobdistPoissonVarianceWithObject(const clprobdistPoisson* dist, clprobdistStatus* err); 113 | 114 | /*! @copydoc clprobdistStdDeviationWithObject() 115 | */ 116 | cl_double clprobdistPoissonStdDeviationWithObject(const clprobdistPoisson* dist, clprobdistStatus* err); 117 | 118 | /*! @brief Return the value of the mean \f$\lambda\f$ [**device**] 119 | */ 120 | cl_double clprobdistPoissonGetLambda(const clprobdistPoisson* dist, clprobdistStatus* err); 121 | 122 | /*! @} */ 123 | 124 | 125 | /*! @name Functions for use with explicit distribution parameters 126 | * 127 | * @{ 128 | */ 129 | 130 | /*! @brief @copybrief clprobdistProb() 131 | * 132 | * @see clprobdistPoissonProbWithObject() 133 | * 134 | * @param[in] lambda Value of the mean \f$\lambda\f$. 135 | * @param[in] x Value of \f$x\f$. 136 | * @param[out] err Error status variable, or \c NULL. 137 | * @return Value of \f$f(x)\f$. 138 | */ 139 | cl_double clprobdistPoissonProb(cl_double lambda, cl_int x, clprobdistStatus* err); 140 | 141 | /*! @brief @copybrief clprobdistCDF() 142 | * 143 | * @see clprobdistPoissonCDFWithObject() 144 | * 145 | * @param[in] lambda Value of the mean \f$\lambda\f$. 146 | * @param[in] x Value of \f$x\f$. 147 | * @param[out] err Error status variable, or \c NULL. 148 | * @return Value of \f$F(x)\f$. 149 | */ 150 | cl_double clprobdistPoissonCDF(cl_double lambda, cl_int x, clprobdistStatus* err); 151 | 152 | /*! @brief @copybrief clprobdistComplCDF() 153 | * 154 | * @see clprobdistPoissonComplCDFWithObject() 155 | * 156 | * @warning The complementary distribution function is defined as 157 | * \f$\bar F(j) = \mathbb P[X \geq j]\f$. 158 | * 159 | * @param[in] lambda Value of the mean \f$\lambda\f$. 160 | * @param[in] x Value of \f$x\f$. 161 | * @param[out] err Error status variable, or \c NULL. 162 | * @return Value of \f$\bar F(x)\f$. 163 | */ 164 | cl_double clprobdistPoissonComplCDF(cl_double lambda, cl_int x, clprobdistStatus* err); 165 | 166 | /*! @brief @copybrief clprobdistInverseCDF() 167 | * 168 | * @see clprobdistPoissonInverseCDFWithObject() 169 | * 170 | * @param[in] lambda Value of the mean \f$\lambda\f$. 171 | * @param[in] u Value of \f$u \in [0,1]\f$. 172 | * @param[out] err Error status variable, or \c NULL. 173 | * @return Value of \f$F^{-1}(u)\f$. 174 | */ 175 | cl_int clprobdistPoissonInverseCDF(cl_double lambda, cl_double u, clprobdistStatus* err); 176 | 177 | /*! @brief @copybrief clprobdistMean() 178 | * 179 | * @see clprobdistPoissonMeanWithObject() 180 | * 181 | * @param[in] lambda Value of the mean \f$\lambda\f$. 182 | * @param[out] err Error status variable, or \c NULL. 183 | * @return Mean of the distribution. 184 | */ 185 | cl_double clprobdistPoissonMean(cl_double lambda, clprobdistStatus* err); 186 | 187 | /*! @brief @copybrief clprobdistVariance() 188 | * 189 | * @see clprobdistPoissonVarianceWithObject() 190 | * 191 | * @param[in] lambda Value of the mean \f$\lambda\f$. 192 | * @param[out] err Error status variable, or \c NULL. 193 | * @return Variance of the distribution. 194 | */ 195 | cl_double clprobdistPoissonVariance(cl_double lambda, clprobdistStatus* err); 196 | 197 | /*! @brief @copybrief clprobdistStdDeviation() 198 | * 199 | * @see clprobdistPoissonStdDeviationWithObject() 200 | * 201 | * @param[in] lambda Value of the mean \f$\lambda\f$. 202 | * @param[out] err Error status variable, or \c NULL. 203 | * @return Standard deviation of the distribution. 204 | */ 205 | cl_double clprobdistPoissonStdDeviation(cl_double lambda, clprobdistStatus* err); 206 | 207 | /*! @} */ 208 | 209 | 210 | /*! @privatesection 211 | */ 212 | 213 | /*! @copydoc clprobdistInverseCDF() 214 | */ 215 | cl_int clprobdistDiscreteInverseCDF(const clprobdistPoisson* dist, cl_double u, clprobdistStatus* err); 216 | 217 | #endif /* POISSONDIST_H */ 218 | -------------------------------------------------------------------------------- /src/include/clProbDist/private/exponential.c.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #pragma once 26 | #ifndef CLPROBDIST_PRIVATE_EXPONENTIALDIST_CH 27 | #define CLPROBDIST_PRIVATE_EXPONENTIALDIST_CH 28 | 29 | #ifndef _CLPROBDIST_EXPONENTIAL_OBJ_MEM 30 | #define _CLPROBDIST_EXPONENTIAL_OBJ_MEM 31 | #endif 32 | 33 | struct _clprobdistExponential { 34 | clprobdistContinuous continuousDistObj; 35 | cl_double lambda; 36 | }; 37 | 38 | 39 | constant const cl_double clprobdistExponentialXBIG = 100.0; 40 | constant const cl_double clprobdistExponentialXBIGM = 1000.0; 41 | 42 | clprobdistStatus clprobdistExponentialSetLambda(_CLPROBDIST_EXPONENTIAL_OBJ_MEM clprobdistExponential* distObj, cl_double newlambda) 43 | { 44 | clprobdistStatus err = CLPROBDIST_SUCCESS; 45 | 46 | if (distObj->lambda <= 0) 47 | return clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): lambda <= 0", __func__); 48 | 49 | distObj->lambda = newlambda; 50 | distObj->continuousDistObj.supportA = 0.0; 51 | 52 | return err; 53 | } 54 | 55 | cl_double clprobdistExponentialDensity(cl_double lambda, cl_double x, clprobdistStatus* err) 56 | { 57 | if (err) *err = CLPROBDIST_SUCCESS; 58 | 59 | if (lambda <= 0){ 60 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): lambda <= 0", __func__); 61 | return -1; 62 | } 63 | 64 | return x < 0 ? 0 : lambda * exp(-lambda * x); 65 | } 66 | 67 | cl_double clprobdistExponentialCDF(cl_double lambda, cl_double x, clprobdistStatus* err) 68 | { 69 | if (err) *err = CLPROBDIST_SUCCESS; 70 | 71 | if (lambda <= 0){ 72 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): lambda <= 0", __func__); 73 | return -1; 74 | } 75 | 76 | if (x <= 0.0) 77 | return 0.0; 78 | 79 | cl_double y = lambda * x; 80 | if (y >= clprobdistExponentialXBIG) 81 | return 1.0; 82 | 83 | return -expm1(-y); 84 | } 85 | 86 | cl_double clprobdistExponentialComplCDF(cl_double lambda, cl_double x, clprobdistStatus* err) 87 | { 88 | if (err) *err = CLPROBDIST_SUCCESS; 89 | 90 | if (lambda <= 0){ 91 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): lambda <= 0", __func__); 92 | return -1; 93 | } 94 | if (x <= 0.0) 95 | return 1.0; 96 | 97 | if (lambda*x >= clprobdistExponentialXBIGM) 98 | return 0.0; 99 | 100 | return exp(-lambda*x); 101 | } 102 | 103 | cl_double clprobdistExponentialInverseCDF(cl_double lambda, cl_double u, clprobdistStatus* err){ 104 | 105 | if (err) *err = CLPROBDIST_SUCCESS; 106 | if (lambda <= 0){ 107 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): lambda <= 0", __func__); 108 | return -1; 109 | } 110 | 111 | if (u < 0.0 || u > 1.0) { 112 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): u not in [0,1]", __func__); 113 | return -1; 114 | } 115 | 116 | if (u >= 1.0) 117 | return DBL_MAX; 118 | 119 | if (u <= 0.0) 120 | return 0.0; 121 | 122 | return -log1p(-u) / lambda; // log1p is defined on OpenCL C 123 | } 124 | 125 | cl_double clprobdistExponentialMean(cl_double lambda, clprobdistStatus* err) 126 | { 127 | if (err) *err = CLPROBDIST_SUCCESS; 128 | if (lambda <= 0.0){ 129 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): lambda <= 0", __func__); 130 | return -1; 131 | } 132 | 133 | return (1 / lambda); 134 | } 135 | 136 | cl_double clprobdistExponentialVariance(cl_double lambda, clprobdistStatus* err) 137 | { 138 | if (err) *err = CLPROBDIST_SUCCESS; 139 | if (lambda <= 0.0){ 140 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): lambda <= 0", __func__); 141 | return -1; 142 | } 143 | 144 | return (1 / (lambda * lambda)); 145 | } 146 | 147 | cl_double clprobdistExponentialStdDeviation(cl_double lambda, clprobdistStatus* err) 148 | { 149 | *err = CLPROBDIST_SUCCESS; 150 | if (lambda <= 0.0){ 151 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): lambda <= 0", __func__); 152 | return -1; 153 | } 154 | 155 | return (1 / lambda); 156 | } 157 | 158 | 159 | cl_double clprobdistExponentialGetLambda(_CLPROBDIST_EXPONENTIAL_OBJ_MEM const clprobdistExponential* distObj, clprobdistStatus* err) 160 | { 161 | if (err) *err = CLPROBDIST_SUCCESS; 162 | return distObj->lambda; 163 | } 164 | 165 | 166 | cl_double clprobdistExponentialDensityWithObject(_CLPROBDIST_EXPONENTIAL_OBJ_MEM const clprobdistExponential* distObj, cl_double x, clprobdistStatus* err) { 167 | return clprobdistExponentialDensity(distObj->lambda, x, err); 168 | } 169 | 170 | cl_double clprobdistExponentialCDFWithObject(_CLPROBDIST_EXPONENTIAL_OBJ_MEM const clprobdistExponential* distObj, cl_double x, clprobdistStatus* err) { 171 | return clprobdistExponentialCDF(distObj->lambda, x, err); 172 | } 173 | 174 | cl_double clprobdistExponentialComplCDFWithObject(_CLPROBDIST_EXPONENTIAL_OBJ_MEM const clprobdistExponential* distObj, cl_double x, clprobdistStatus* err) { 175 | return clprobdistExponentialComplCDF(distObj->lambda, x, err); 176 | } 177 | 178 | cl_double clprobdistExponentialInverseCDFWithObject(_CLPROBDIST_EXPONENTIAL_OBJ_MEM const clprobdistExponential* distObj, cl_double u, clprobdistStatus* err) { 179 | return clprobdistExponentialInverseCDF(distObj->lambda, u, err); 180 | } 181 | 182 | 183 | cl_double clprobdistExponentialMeanWithObject(_CLPROBDIST_EXPONENTIAL_OBJ_MEM const clprobdistExponential* distObj, clprobdistStatus* err){ 184 | return clprobdistExponentialMean(distObj->lambda, err); 185 | } 186 | 187 | cl_double clprobdistExponentialVarianceWithObject(_CLPROBDIST_EXPONENTIAL_OBJ_MEM const clprobdistExponential* distObj, clprobdistStatus* err){ 188 | return clprobdistExponentialVariance(distObj->lambda, err); 189 | } 190 | 191 | cl_double clprobdistExponentialStdDeviationWithObject(_CLPROBDIST_EXPONENTIAL_OBJ_MEM const clprobdistExponential* distObj, clprobdistStatus* err){ 192 | return clprobdistExponentialStdDeviation(distObj->lambda, err); 193 | } 194 | 195 | 196 | #endif 197 | -------------------------------------------------------------------------------- /src/include/clProbDist/private/lognormal.c.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #pragma once 26 | #ifndef CLPROBDIST_PRIVATE_LOGNORMALDIST_CH 27 | #define CLPROBDIST_PRIVATE_LOGNORMALDIST_CH 28 | 29 | #ifndef _CLPROBDIST_LOGNORMAL_OBJ_MEM 30 | #define _CLPROBDIST_LOGNORMAL_OBJ_MEM 31 | #endif 32 | 33 | struct _clprobdistlLognormal { 34 | clprobdistContinuous contDistObj; 35 | cl_double mu; 36 | cl_double sigma; 37 | }; 38 | 39 | constant cl_double clprobdistLognormalLN2 = 0.6931471805599453; 40 | constant cl_double clprobdistLognormalPI = 3.14159265358979323846; 41 | 42 | constant const cl_double clprobdistLognormalXBIG = 100.0; 43 | constant const cl_double clprobdistLognormalXBIGM = 1000.0; 44 | 45 | //*********************************************************************** 46 | // Implementation of static functions 47 | //*********************************************************************** 48 | 49 | cl_double clprobdistLognormalDensity(cl_double mu, cl_double sigma, cl_double x, clprobdistStatus* err) { 50 | if (sigma <= 0.0){ 51 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): sigma <= 0", __func__); 52 | return 0; 53 | } 54 | if (x <= 0) 55 | return 0; 56 | 57 | cl_double diff = log(x) - mu; 58 | 59 | return exp(-diff*diff / (2 * sigma*sigma)) / (sqrt(2 * clprobdistLognormalPI)*sigma*x); 60 | } 61 | 62 | cl_double clprobdistLognormalCDF(cl_double mu, cl_double sigma, cl_double x, clprobdistStatus* err) { 63 | if (sigma <= 0.0){ 64 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): sigma <= 0", __func__); 65 | return 0; 66 | } 67 | 68 | if (x <= 0.0) 69 | return 0.0; 70 | return clprobdistStdNormalCDF((log(x) - mu) / sigma, err); 71 | } 72 | cl_double clprobdistLognormalComplCDF(cl_double mu, cl_double sigma, cl_double x, clprobdistStatus* err) { 73 | if (sigma <= 0.0){ 74 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): sigma <= 0", __func__); 75 | return 0; 76 | } 77 | if (x <= 0.0) 78 | return 1.0; 79 | return clprobdistStdNormalComplCDF((log(x) - mu) / sigma, err); 80 | } 81 | cl_double clprobdistLognormalInverseCDF(cl_double mu, cl_double sigma, cl_double u, clprobdistStatus* err) { 82 | cl_double t, v; 83 | 84 | if (sigma <= 0.0){ 85 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): sigma <= 0", __func__); 86 | return 0; 87 | } 88 | 89 | if (u > 1.0 || u < 0.0){ 90 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): u is not in [0, 1]", __func__); 91 | return -1; 92 | } 93 | 94 | if (DBL_EPSILON >= 1.0 - u) 95 | return DBL_MAX; // Double.POSITIVE_INFINITY; 96 | 97 | if (u <= 0.0) 98 | return 0.0; 99 | 100 | t = clprobdistStdNormalInverseCDF(u, err); // NormalDist.inverseF01(u); 101 | v = mu + sigma * t; 102 | 103 | if ((t >= clprobdistLognormalXBIG) || (v >= DBL_MAX_EXP * clprobdistLognormalLN2)) 104 | return DBL_MAX; 105 | if ((t <= -clprobdistLognormalXBIG) || (v <= -DBL_MAX_EXP* clprobdistLognormalLN2)) 106 | return 0.0; 107 | 108 | return exp(v); 109 | } 110 | cl_double clprobdistLognormalMean(cl_double mu, cl_double sigma, clprobdistStatus* err) { 111 | if (sigma <= 0.0){ 112 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): sigma <= 0", __func__); 113 | return 0; 114 | } 115 | 116 | return (exp(mu + (sigma * sigma) / 2.0)); 117 | } 118 | cl_double clprobdistLognormalVariance(cl_double mu, cl_double sigma, clprobdistStatus* err) { 119 | if (sigma <= 0.0){ 120 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): sigma <= 0", __func__); 121 | return 0; 122 | } 123 | 124 | return (exp(2.0 * mu + sigma * sigma) * (exp(sigma * sigma) - 1.0)); 125 | } 126 | cl_double clprobdistLognormalStdDeviation(cl_double mu, cl_double sigma, clprobdistStatus* err) { 127 | 128 | return sqrt(clprobdistLognormalVariance(mu, sigma, err)); 129 | } 130 | 131 | //*********************************************************************** 132 | // Implementation with Dist object 133 | //*********************************************************************** 134 | 135 | cl_double clprobdistLognormalDensityWithObject(_CLPROBDIST_LOGNORMAL_OBJ_MEM const clprobdistLognormal* dist, cl_double x, clprobdistStatus* err) { 136 | return clprobdistLognormalDensity(dist->mu, dist->sigma, x, err); 137 | } 138 | 139 | cl_double clprobdistLognormalCDFWithObject(_CLPROBDIST_LOGNORMAL_OBJ_MEM const clprobdistLognormal* dist, cl_double x, clprobdistStatus* err) { 140 | return clprobdistLognormalCDF(dist->mu, dist->sigma, x, err); 141 | } 142 | 143 | cl_double clprobdistLognormalComplCDFWithObject(_CLPROBDIST_LOGNORMAL_OBJ_MEM const clprobdistLognormal* dist, cl_double x, clprobdistStatus* err) { 144 | return clprobdistLognormalComplCDF(dist->mu, dist->sigma, x, err); 145 | } 146 | 147 | cl_double clprobdistLognormalInverseCDFWithObject(_CLPROBDIST_LOGNORMAL_OBJ_MEM const clprobdistLognormal* dist, cl_double u, clprobdistStatus* err) { 148 | return clprobdistLognormalInverseCDF(dist->mu, dist->sigma, u, err); 149 | } 150 | 151 | cl_double clprobdistLognormalMeanWithObject(_CLPROBDIST_LOGNORMAL_OBJ_MEM const clprobdistLognormal* dist, clprobdistStatus* err) { 152 | return clprobdistLognormalMean(dist->mu, dist->sigma, err); 153 | } 154 | 155 | cl_double clprobdistLognormalVarianceWithObject(_CLPROBDIST_LOGNORMAL_OBJ_MEM const clprobdistLognormal* dist, clprobdistStatus* err) { 156 | return clprobdistLognormalVariance(dist->mu, dist->sigma, err); 157 | } 158 | 159 | cl_double clprobdistLognormalStdDeviationWithObject(_CLPROBDIST_LOGNORMAL_OBJ_MEM const clprobdistLognormal* dist, clprobdistStatus* err) { 160 | return clprobdistLognormalStdDeviation(dist->mu, dist->sigma, err); 161 | } 162 | 163 | cl_double clprobdistLognormalGetMu(_CLPROBDIST_LOGNORMAL_OBJ_MEM const clprobdistLognormal* dist, clprobdistStatus* err) { 164 | if (err) *err = CLPROBDIST_SUCCESS; 165 | return dist->mu; 166 | } 167 | 168 | cl_double clprobdistLognormalGetSigma(_CLPROBDIST_LOGNORMAL_OBJ_MEM const clprobdistLognormal* dist, clprobdistStatus* err) { 169 | if (err) *err = CLPROBDIST_SUCCESS; 170 | return dist->sigma; 171 | } 172 | #endif 173 | -------------------------------------------------------------------------------- /src/library/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # ######################################################################## 2 | # Copyright 2013 Advanced Micro Devices, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ######################################################################## 16 | 17 | # List the names of common files to compile across all platforms 18 | set( clProbDist.Source clProbDist.c 19 | private.c 20 | continuous.c 21 | exponential.c 22 | gamma.c 23 | normal.c 24 | lognormal.c 25 | poisson.c 26 | ) 27 | 28 | if( MSVC ) 29 | # Use C++ with Microsoft compiler 30 | SET_SOURCE_FILES_PROPERTIES( ${clProbDist.Source} PROPERTIES LANGUAGE CXX) 31 | endif( ) 32 | 33 | # Windows only uses dllmain 34 | #if( MSVC ) 35 | # set( clProbDist.Source ${clProbDist.Source} dllmain.cpp ) 36 | #endif( ) 37 | 38 | set( clProbDist.Headers private.h 39 | ../include/clProbDist/clProbDist.h 40 | ../include/clProbDist/clProbDist.version.h 41 | ../include/clProbDist/continuous.h 42 | ../include/clProbDist/exponential.h 43 | ../include/clProbDist/gamma.h 44 | ../include/clProbDist/normal.h 45 | ../include/clProbDist/lognormal.h 46 | ../include/clProbDist/poisson.h 47 | ) 48 | 49 | set( clProbDist.Files ${clProbDist.Source} ${clProbDist.Headers} ) 50 | 51 | # Include standard OpenCL headers 52 | include_directories( ${OPENCL_INCLUDE_DIRS} ${PROJECT_BINARY_DIR}/include ../include ) 53 | 54 | if(BUILD_SHARED_LIBRARY) 55 | add_library( clProbDist SHARED ${clProbDist.Files} ) 56 | else() 57 | add_library( clProbDist STATIC ${clProbDist.Files} ) 58 | endif() 59 | 60 | target_link_libraries( clProbDist ${OPENCL_LIBRARIES} ) 61 | 62 | set_target_properties( clProbDist PROPERTIES VERSION ${CLPROBDIST_VERSION} ) 63 | set_target_properties( clProbDist PROPERTIES SOVERSION ${CLPROBDIST_SOVERSION} ) 64 | set_target_properties( clProbDist PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/staging" ) 65 | 66 | if( CMAKE_COMPILER_IS_GNUCC ) 67 | configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/clProbDist.pc.in 68 | ${CMAKE_CURRENT_BINARY_DIR}/clProbDist.pc @ONLY ) 69 | 70 | install( FILES ${CMAKE_CURRENT_BINARY_DIR}/clProbDist.pc 71 | DESTINATION lib${SUFFIX_LIB}/pkgconfig ) 72 | endif( ) 73 | 74 | # CPack configuration; include the executable into the package 75 | install( TARGETS clProbDist 76 | EXPORT clProbDist-Targets 77 | RUNTIME DESTINATION bin${SUFFIX_BIN} 78 | LIBRARY DESTINATION lib${SUFFIX_LIB} 79 | ARCHIVE DESTINATION lib${SUFFIX_LIB}/import 80 | ) 81 | 82 | # CMake config files for clProbDist 83 | include( CMakePackageConfigHelpers ) 84 | 85 | set( LIB_INSTALL_DIR lib${SUFFIX_LIB} ) 86 | set( INCLUDE_INSTALL_DIR include ) 87 | set( ConfigPackageLocation ${LIB_INSTALL_DIR}/cmake/clProbDist ) 88 | 89 | configure_package_config_file( 90 | clProbDistConfig.cmake.in 91 | ${CMAKE_CURRENT_BINARY_DIR}/clProbDistConfig.cmake 92 | INSTALL_DESTINATION ${ConfigPackageLocation} 93 | PATH_VARS INCLUDE_INSTALL_DIR LIB_INSTALL_DIR 94 | ) 95 | 96 | write_basic_package_version_file( 97 | ${CMAKE_CURRENT_BINARY_DIR}/clProbDistConfigVersion.cmake 98 | VERSION ${CLPROBDIST_VERSION} 99 | COMPATIBILITY SameMajorVersion 100 | ) 101 | 102 | # This generates the files that defines the import targets 103 | install( EXPORT clProbDist-Targets 104 | DESTINATION 105 | ${ConfigPackageLocation} 106 | ) 107 | 108 | #Copy the config files generated by configure_package_config_file & write_basic_package_version_file 109 | install(FILES 110 | ${CMAKE_CURRENT_BINARY_DIR}/clProbDistConfig.cmake 111 | ${CMAKE_CURRENT_BINARY_DIR}/clProbDistConfigVersion.cmake 112 | DESTINATION 113 | ${ConfigPackageLocation} ) 114 | 115 | # For debug builds, include the debug runtimes into the package for testing on non-developer machines 116 | set( CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP true ) 117 | set( CMAKE_INSTALL_DEBUG_LIBRARIES true ) 118 | set( CMAKE_INSTALL_DEBUG_LIBRARIES_ONLY true ) 119 | 120 | if( WIN32 ) 121 | set( CLPROBDIST_RUNTIME_DESTINATION bin${SUFFIX_BIN} ) 122 | else( ) 123 | set( CLPROBDIST_RUNTIME_DESTINATION lib${SUFFIX_LIB} ) 124 | endif( ) 125 | 126 | include( InstallRequiredSystemLibraries ) 127 | 128 | # Install necessary runtime files for debug builds 129 | install( PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} 130 | CONFIGURATIONS Debug 131 | DESTINATION ${CLPROBDIST_RUNTIME_DESTINATION} ) 132 | 133 | # Install all *.pdb files for debug builds 134 | install( DIRECTORY ${PROJECT_BINARY_DIR}/staging/ 135 | DESTINATION ${CLPROBDIST_RUNTIME_DESTINATION} 136 | OPTIONAL 137 | CONFIGURATIONS Debug 138 | FILES_MATCHING PATTERN "*.pdb" ) 139 | 140 | # Install a snapshot of the source as it was for this build; useful for the .pdb's 141 | install( DIRECTORY ${PROJECT_SOURCE_DIR} 142 | DESTINATION ${CLPROBDIST_RUNTIME_DESTINATION} 143 | OPTIONAL 144 | CONFIGURATIONS Debug ) 145 | -------------------------------------------------------------------------------- /src/library/clProbDist.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | /* @file clProbDist.c 26 | * @brief Implementation of functions defined in clProbDist.h and private.h 27 | */ 28 | 29 | #include 30 | #include "private.h" 31 | #include 32 | #include 33 | #include 34 | #ifdef _MSC_VER 35 | #include 36 | #else 37 | #include 38 | #endif 39 | 40 | #define CASE_ERR_(code,msg) case code: base = msg; break 41 | #define CASE_ERR(code) CASE_ERR_(CLPROBDIST_ ## code, MSG_ ## code) 42 | 43 | extern char errorString[1024]; 44 | 45 | const char* clprobdistGetErrorString() 46 | { 47 | return errorString; 48 | } 49 | 50 | 51 | static char lib_path_default1[] = "/usr"; 52 | static char lib_path_default1_check[] = "/usr/include/clProbDist/clProbDist.h"; 53 | static char lib_path_default2[] = "."; 54 | static char lib_path_default2_check[] = "./include/clProbDist/clProbDist.h"; 55 | 56 | const char* clprobdistGetLibraryRoot() 57 | { 58 | const char* lib_path = getenv("CLPROBDIST_ROOT"); 59 | 60 | if (lib_path != NULL && lib_path[0] != 0) 61 | return lib_path; 62 | 63 | // check if lib_path_default1_check exists 64 | if ( 65 | #ifdef _MSC_VER 66 | _access(lib_path_default1_check, 0) != -1 67 | #else 68 | access(lib_path_default1_check, F_OK) != -1 69 | #endif 70 | ) 71 | return lib_path_default1; 72 | 73 | // last resort 74 | if ( 75 | #ifdef _MSC_VER 76 | _access(lib_path_default2_check, 0) != -1 77 | #else 78 | access(lib_path_default2_check, F_OK) != -1 79 | #endif 80 | ) 81 | return lib_path_default2; 82 | 83 | return NULL; 84 | } 85 | 86 | 87 | static char lib_includes[1024]; 88 | 89 | const char* clprobdistGetLibraryDeviceIncludes(cl_int* err) 90 | { 91 | int nbytes; 92 | const char* root = clprobdistGetLibraryRoot(); 93 | 94 | if (err) 95 | *err = CLPROBDIST_SUCCESS; 96 | 97 | if (root == NULL) { 98 | if (err) 99 | *err = clprobdistSetErrorString(CLPROBDIST_INVALID_ENVIRONMENT, "environment variable CLPROBDIST_ROOT not set"); 100 | return NULL; 101 | } 102 | #ifdef _MSC_VER 103 | nbytes = sprintf_s( 104 | #else 105 | nbytes = snprintf( 106 | #endif 107 | lib_includes, 108 | sizeof(lib_includes), 109 | "-I\"%s/include\"", 110 | root); 111 | 112 | #ifdef _MSC_VER 113 | if (nbytes < 0) { 114 | #else 115 | if (nbytes >= sizeof(lib_includes)) { 116 | #endif 117 | if (err) 118 | *err = clprobdistSetErrorString(CLPROBDIST_OUT_OF_RESOURCES, "value of CLPROBDIST_ROOT too long (max = %u)", sizeof(lib_includes) - 16); 119 | return NULL; 120 | } 121 | return lib_includes; 122 | } 123 | -------------------------------------------------------------------------------- /src/library/clProbDist.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=${prefix}/bin@SUFFIX_BIN@ 3 | includedir=${prefix}/include 4 | libdir=${prefix}/lib@SUFFIX_LIB@ 5 | 6 | Name: clProbDist 7 | Description: Open source OpenCL probability distributions library 8 | Version: @CLPROBDIST_VERSION@ 9 | URL: https://github.com/clMathLibraries/clProbDist 10 | 11 | Cflags: -I${includedir} 12 | Libs: -L${libdir} -lclProbDist 13 | -------------------------------------------------------------------------------- /src/library/clProbDistConfig.cmake.in: -------------------------------------------------------------------------------- 1 | # ######################################################################## 2 | # Copyright 2015 Advanced Micro Devices, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ######################################################################## 16 | 17 | # Configure the clProbDist package to be used in another cmake project. 18 | # 19 | # Defines the following variables: 20 | # 21 | # clProbDist_INCLUDE_DIRS - include directories for clProbDist 22 | # 23 | # Also defines the library variables below as normal 24 | # variables. These contain debug/optimized keywords when 25 | # a debugging library is found. 26 | # 27 | # Accepts the following variables as input: 28 | # 29 | #----------------------- 30 | # Example Usage: 31 | # 32 | # find_package( clProbDist REQUIRED CONFIG 33 | # HINTS /package ) 34 | # 35 | # add_executable( foo foo.cc ) 36 | 37 | # # uses imported targets from package, including setting header paths 38 | # target_link_libraries( foo clProbDist ) 39 | # 40 | #----------------------- 41 | 42 | @PACKAGE_INIT@ 43 | 44 | set_and_check( clProbDist_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@" ) 45 | set_and_check( clProbDist_INCLUDE_DIRS "${clProbDist_INCLUDE_DIR}" ) 46 | set_and_check( clProbDist_LIB_INSTALL_DIR "@PACKAGE_LIB_INSTALL_DIR@" ) 47 | 48 | include( "${CMAKE_CURRENT_LIST_DIR}/clProbDist-Targets.cmake" ) 49 | 50 | -------------------------------------------------------------------------------- /src/library/continuous.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | #include "../include/clProbDist/clProbDist.h" 29 | #include "private.h" 30 | #include "../include/clProbDist/continuous.h" 31 | 32 | // x infinity for some distributions 33 | const cl_double clprobdistXBIG = 100.0; 34 | const cl_double clprobdistXBIGM = 1000.0; 35 | 36 | const cl_double clprobdistEPSARRAY[] = { 37 | 0.5, 0.5E-1, 0.5E-2, 0.5E-3, 0.5E-4, 0.5E-5, 0.5E-6, 0.5E-7, 0.5E-8, 38 | 0.5E-9, 0.5E-10, 0.5E-11, 0.5E-12, 0.5E-13, 0.5E-14, 0.5E-15, 0.5E-16, 39 | 0.5E-17, 0.5E-18, 0.5E-19, 0.5E-20, 0.5E-21, 0.5E-22, 0.5E-23, 0.5E-24, 40 | 0.5E-25, 0.5E-26, 0.5E-27, 0.5E-28, 0.5E-29, 0.5E-30, 0.5E-31, 0.5E-32, 41 | 0.5E-33, 0.5E-34, 0.5E-35 42 | }; 43 | 44 | // code that is common to host and device 45 | #include "../include/clProbDist/private/continuous.c.h" 46 | 47 | -------------------------------------------------------------------------------- /src/library/exponential.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "../include/clProbDist/clProbDist.h" 30 | #include "private.h" 31 | #include "../include/clProbDist/exponential.h" 32 | 33 | // x infinity for some distributions 34 | //extern const cl_double clprobdistXBIG; 35 | //extern const cl_double clprobdistXBIGM; 36 | 37 | // code that is common to host and device 38 | #include "../include/clProbDist/private/exponential.c.h" 39 | 40 | clprobdistExponential* clprobdistExponentialCreate(cl_double lambda, size_t* bufSize, clprobdistStatus* err) 41 | { 42 | clprobdistExponential* expoDistObj = (clprobdistExponential*)malloc(sizeof(clprobdistExponential)); 43 | 44 | if (!expoDistObj) { 45 | if (err) 46 | *err = clprobdistSetErrorString(CLPROBDIST_OUT_OF_RESOURCES, "%s(): could not allocate memory for distribution", __func__); 47 | return NULL; 48 | } 49 | 50 | if (bufSize) 51 | *bufSize = sizeof(clprobdistExponential); 52 | if (err) 53 | *err = CLPROBDIST_SUCCESS; 54 | 55 | expoDistObj->continuousDistObj.decPrec = 15; 56 | expoDistObj->continuousDistObj.supportA = 0.0; 57 | expoDistObj->continuousDistObj.supportB = DBL_MAX; 58 | expoDistObj->lambda = lambda; 59 | 60 | return expoDistObj; 61 | } 62 | clprobdistStatus clprobdistExponentialDestroy(clprobdistExponential* distObj) 63 | { 64 | if (distObj != NULL) 65 | free(distObj); 66 | 67 | return CLPROBDIST_SUCCESS; 68 | } 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /src/library/gamma.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "../include/clProbDist/clProbDist.h" 30 | #include "private.h" 31 | 32 | #include "../include/clProbDist/continuous.h" 33 | #include "../include/clProbDist/exponential.h" 34 | #include "../include/clProbDist/normal.h" 35 | #include "../include/clProbDist/gamma.h" 36 | 37 | // x infinity for some distributions 38 | //extern const cl_double clprobdistXBIG; 39 | //extern const cl_double clprobdistXBIGM; 40 | 41 | // clprobdistEPSARRAY[j]: Epsilon required for j decimal degits of precision 42 | //extern const cl_double clprobdistEPSARRAY[]; 43 | 44 | // code that is common to host and device 45 | #include "../include/clProbDist/private/gamma.c.h" 46 | 47 | clprobdistGamma* clprobdistGammaCreate(cl_double alpha, cl_double lambda, int decprec, size_t* bufSize, clprobdistStatus* err) 48 | { 49 | clprobdistStatus err_ = CLPROBDIST_SUCCESS; 50 | 51 | //Check params 52 | if (alpha <= 0.0) 53 | err_ = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): alpha <= 0", __func__); 54 | 55 | if (decprec <= 0) 56 | err_ = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): d <= 0", __func__); 57 | 58 | if (lambda <= 0) 59 | err_ = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): lambda <= 0", __func__); 60 | 61 | if (err_ != CLPROBDIST_SUCCESS) { 62 | if (err) *err = err_; 63 | return NULL; 64 | } 65 | 66 | clprobdistGamma* gammaDist = (clprobdistGamma*)malloc(sizeof(clprobdistGamma)); 67 | 68 | if (!gammaDist) { 69 | if (err) 70 | *err = clprobdistSetErrorString(CLPROBDIST_OUT_OF_RESOURCES, "%s(): could not allocate memory for distribution", __func__); 71 | return NULL; 72 | } 73 | 74 | if (bufSize) 75 | *bufSize = sizeof(clprobdistGamma); 76 | if (err) 77 | *err = CLPROBDIST_SUCCESS; 78 | 79 | gammaDist->contDistObj.decPrec = decprec; 80 | gammaDist->contDistObj.supportA = 0.0; 81 | gammaDist->contDistObj.supportB = DBL_MAX; 82 | 83 | gammaDist->params.alpha = alpha; 84 | gammaDist->params.lambda = lambda; 85 | gammaDist->params.logFactor = alpha * log(lambda) - lgamma(alpha); 86 | 87 | return gammaDist; 88 | } 89 | clprobdistStatus clprobdistGammaDestroy(clprobdistGamma* distObj){ 90 | if (distObj != NULL) 91 | free(distObj); 92 | 93 | return CLPROBDIST_SUCCESS; 94 | } 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /src/library/lognormal.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #include 26 | 27 | #include 28 | #include 29 | 30 | #include "../include/clProbDist/clProbDist.h" 31 | #include "private.h" 32 | 33 | #include "../include/clProbDist/continuous.h" 34 | #include "../include/clProbDist/normal.h" 35 | #include "../include/clProbDist/lognormal.h" 36 | 37 | // x infinity for some distributions 38 | //extern const cl_double clprobdistXBIG; 39 | //extern const cl_double clprobdistXBIGM; 40 | 41 | // code that is common to host and device 42 | #include "../include/clProbDist/private/lognormal.c.h" 43 | 44 | clprobdistLognormal* clprobdistLognormalCreate(cl_double mu, cl_double sigma, size_t* bufSize, clprobdistStatus* err) 45 | { 46 | //check params 47 | if (sigma <= 0.0){ 48 | if (*err) 49 | *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): sigma <= 0", __func__); 50 | return NULL; 51 | } 52 | 53 | clprobdistLognormal* dist = (clprobdistLognormal*)malloc(sizeof(clprobdistLognormal)); 54 | 55 | if (!dist) { 56 | if (err) 57 | *err = clprobdistSetErrorString(CLPROBDIST_OUT_OF_RESOURCES, "%s(): could not allocate memory for distribution", __func__); 58 | return NULL; 59 | } 60 | 61 | if (bufSize) 62 | *bufSize = sizeof(clprobdistLognormal); 63 | if (err) 64 | *err = CLPROBDIST_SUCCESS; 65 | 66 | dist->contDistObj.decPrec = 15; 67 | dist->contDistObj.supportA = 0.0; 68 | dist->contDistObj.supportB = DBL_MAX; 69 | dist->mu = mu; 70 | dist->sigma = sigma; 71 | 72 | return dist; 73 | } 74 | 75 | clprobdistStatus clprobdistLognormalDestroy(clprobdistLognormal* dist) 76 | { 77 | if (dist != NULL) 78 | free(dist); 79 | 80 | return CLPROBDIST_SUCCESS; 81 | } 82 | -------------------------------------------------------------------------------- /src/library/normal.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "../include/clProbDist/clProbDist.h" 30 | #include "private.h" 31 | 32 | #include "../include/clProbDist/continuous.h" 33 | #include "../include/clProbDist/normal.h" 34 | 35 | // x infinity for some distributions 36 | //extern const cl_double clprobdistXBIG; 37 | //extern const cl_double clprobdistXBIGM; 38 | 39 | // code that is common to host and device 40 | #include "../include/clProbDist/private/normal.c.h" 41 | 42 | clprobdistNormal* clprobdistNormalCreate(cl_double mu, cl_double sigma, size_t* bufSize, clprobdistStatus* err) 43 | { 44 | //check params 45 | if (sigma <= 0.0){ 46 | if (*err) 47 | *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): sigma <= 0", __func__); 48 | return NULL; 49 | } 50 | 51 | clprobdistNormal* dist = (clprobdistNormal*)malloc(sizeof(clprobdistNormal)); 52 | 53 | if (!dist) { 54 | if (err) 55 | *err = clprobdistSetErrorString(CLPROBDIST_OUT_OF_RESOURCES, "%s(): could not allocate memory for distribution", __func__); 56 | return NULL; 57 | } 58 | 59 | if (bufSize) 60 | *bufSize = sizeof(clprobdistNormal); 61 | if (err) 62 | *err = CLPROBDIST_SUCCESS; 63 | 64 | dist->contDistObj.decPrec = 15; 65 | dist->contDistObj.supportA = DBL_MIN; 66 | dist->contDistObj.supportB = DBL_MAX; 67 | dist->mu = mu; 68 | dist->sigma = sigma; 69 | 70 | return dist; 71 | } 72 | 73 | clprobdistStatus clprobdistNormalDestroy(clprobdistNormal* dist) 74 | { 75 | if (dist != NULL) 76 | free(dist); 77 | 78 | return CLPROBDIST_SUCCESS; 79 | } 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /src/library/poisson.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include "../include/clProbDist/clProbDist.h" 31 | #include "private.h" 32 | 33 | #include "../include/clProbDist/gamma.h" 34 | #include "../include/clProbDist/poisson.h" 35 | 36 | // code that is common to host and device 37 | #include "../include/clProbDist/private/poisson.c.h" 38 | 39 | 40 | const cl_double MAXlambda = 100000; 41 | const cl_double EPS_EXTRA = 1.0e-6; 42 | 43 | /*********************************** 44 | * Constructor & destructor 45 | ***********************************/ 46 | 47 | clprobdistPoisson* clprobdistPoissonInitialize(cl_double lambda, size_t *bufSize, clprobdistStatus* err) { 48 | 49 | //Allocate distribution parameters 50 | clprobdistPoissonParams* params = (clprobdistPoissonParams*)malloc(sizeof(clprobdistPoissonParams)); 51 | 52 | if (!params) { 53 | if (err) 54 | *err = clprobdistSetErrorString(CLPROBDIST_OUT_OF_RESOURCES, "%s(): could not allocate memory for distribution", __func__); 55 | return NULL; 56 | } 57 | 58 | params->supportA = 0; 59 | params->supportB = INT_MAX; 60 | 61 | if (lambda < 0.0){ 62 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): lambda < 0", __func__); 63 | return NULL; 64 | } 65 | 66 | params->lambda = lambda; 67 | 68 | 69 | // For lambda > MAXlambda, we do not use pre-computed arrays 70 | if (lambda > MAXlambda) { 71 | //dist->pdf = NULL; 72 | //dist->cdf = NULL; 73 | if (err) *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): lambda > MAXlambda ", __func__); 74 | return NULL; 75 | } 76 | 77 | cl_double epsilon; 78 | int i, mid, Nmax; 79 | int imin, imax; 80 | cl_double sum; 81 | cl_double* P; // Poisson probability terms 82 | cl_double* F; // Poisson cumulative probabilities 83 | 84 | // In theory, the Poisson distribution has an infinite range. But 85 | // for i > Nmax, probabilities should be extremely small. 86 | Nmax = (int)(lambda + 16 * (2 + sqrt(lambda))); 87 | P = (cl_double*)malloc((1 + Nmax)*sizeof(cl_double)); 88 | if (!P) { 89 | if (err) 90 | *err = clprobdistSetErrorString(CLPROBDIST_OUT_OF_RESOURCES, "%s(): could not allocate memory for distribution", __func__); 91 | return NULL; 92 | } 93 | 94 | mid = (int)lambda; 95 | epsilon = clprobdistPoisson_EPSILON * EPS_EXTRA / clprobdistPoissonProb(lambda, mid, err); 96 | 97 | // For large lambda, mass will lose a few digits of precision 98 | // We shall normalize by explicitly summing all terms >= epsilon 99 | sum = P[mid] = 1.0; 100 | 101 | // Start from the maximum and compute terms > epsilon on each side. 102 | i = mid; 103 | while (i > 0 && P[i] > epsilon) { 104 | P[i - 1] = P[i] * i / lambda; 105 | i--; 106 | sum += P[i]; 107 | } 108 | params->xmin = imin = i; 109 | 110 | i = mid; 111 | while (P[i] > epsilon) { 112 | P[i + 1] = P[i] * lambda / (i + 1); 113 | i++; 114 | sum += P[i]; 115 | if (i >= Nmax - 1) { 116 | Nmax *= 2; 117 | cl_double* nT = (cl_double*)malloc((1 + Nmax) * sizeof(cl_double)); 118 | if (!nT) { 119 | if (err) 120 | *err = clprobdistSetErrorString(CLPROBDIST_OUT_OF_RESOURCES, "%s(): could not allocate memory for distribution", __func__); 121 | return NULL; 122 | } 123 | arraycopy(P, 0, nT, 0, (1 + Nmax) /*P.length*/); 124 | P = nT; 125 | } 126 | } 127 | params->xmax = imax = i; 128 | F = (cl_double*)malloc((1 + Nmax)*sizeof(cl_double)); 129 | if (!F) { 130 | if (err) 131 | *err = clprobdistSetErrorString(CLPROBDIST_OUT_OF_RESOURCES, "%s(): could not allocate memory for distribution", __func__); 132 | return NULL; 133 | } 134 | 135 | // Renormalize the sum of probabilities to 1 136 | for (i = imin; i <= imax; i++) 137 | P[i] /= sum; 138 | 139 | // Compute the cumulative probabilities until F >= 0.5, and keep them in 140 | // the lower part of array, i.e. F[s] contains all P[i] for i <= s 141 | F[imin] = P[imin]; 142 | i = imin; 143 | while (i < imax && F[i] < 0.5) { 144 | i++; 145 | F[i] = P[i] + F[i - 1]; 146 | } 147 | // This is the boundary between F and 1 - F in the CDF 148 | params->xmed = i; 149 | 150 | // Compute the cumulative probabilities of the complementary distribution 151 | // and keep them in the upper part of the array. i.e. F[s] contains all 152 | // P[i] for i >= s 153 | F[imax] = P[imax]; 154 | i = imax - 1; 155 | do { 156 | F[i] = P[i] + F[i + 1]; 157 | i--; 158 | } while (i > params->xmed); 159 | 160 | /* Reset imin because we lose too much precision for a few terms near 161 | imin when we stop adding terms < epsilon. */ 162 | i = imin; 163 | while (i < params->xmed && F[i] < clprobdistPoisson_EPSILON) 164 | i++; 165 | params->xmin = imin = i; 166 | 167 | /* Same thing with imax */ 168 | i = imax; 169 | while (i > params->xmed && F[i] < clprobdistPoisson_EPSILON) 170 | i--; 171 | params->xmax = imax = i; 172 | 173 | // Create the Poisson Distribution with flex array : 174 | params->len = (imax + 1 - imin); 175 | size_t bufSize_ = sizeof(clprobdistPoisson) + 2 * ((params->len - 1) * sizeof(cl_double)); 176 | clprobdistPoisson* dist = (clprobdistPoisson*)malloc(bufSize_); 177 | if (!dist) { 178 | if (err) 179 | *err = clprobdistSetErrorString(CLPROBDIST_OUT_OF_RESOURCES, "%s(): could not allocate memory for distribution", __func__); 180 | return NULL; 181 | } 182 | 183 | //Set dist values: 184 | dist->params = *params; 185 | 186 | //Copy CDF and PDF values 187 | for (int i = 0; i < params->len; i++){ 188 | dist->pdf[i] = P[imin + i]; 189 | dist->cdf[i + (params->len - 1)] = F[imin + i]; 190 | } 191 | 192 | /*int len = params->len; 193 | for (unsigned ix = 0; ix < len; ix++) 194 | printf("pdf %d , %20.19f \n", ix, dist->pdf[ix]); 195 | 196 | for (unsigned ix = 0; ix < len; ix++) 197 | printf("cdf %d , %20.19f \n", ix, dist->cdf[ix + (len - 1)]);*/ 198 | 199 | if (bufSize) 200 | *bufSize = bufSize_; 201 | if (err) 202 | *err = CLPROBDIST_SUCCESS; 203 | 204 | return dist; 205 | } 206 | 207 | clprobdistPoisson* clprobdistPoissonCreate(cl_double lambda, size_t* bufSize, clprobdistStatus* err) 208 | { 209 | //check params 210 | if (lambda <= 0.0){ 211 | if (err) 212 | *err = clprobdistSetErrorString(CLPROBDIST_INVALID_VALUE, "%s(): alpha <= 0", __func__); 213 | return NULL; 214 | } 215 | 216 | //Initialize the params and histogrammes 217 | clprobdistPoisson* dist = clprobdistPoissonInitialize(lambda, bufSize, err); 218 | 219 | return dist; 220 | } 221 | clprobdistStatus clprobdistPoissonDestroy(clprobdistPoisson* dist) 222 | { 223 | if (dist != NULL) 224 | free(dist); 225 | 226 | return CLPROBDIST_SUCCESS; 227 | } 228 | 229 | -------------------------------------------------------------------------------- /src/library/private.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | /* @file private.c 26 | * @brief Implementation of functions defined in clProbDist.h and private.h 27 | */ 28 | #include 29 | #include "private.h" 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #define CASE_ERR_(code,msg) case code: base = msg; break 36 | #define CASE_ERR(code) CASE_ERR_(CLPROBDIST_ ## code, MSG_ ## code) 37 | 38 | char errorString[1024] = ""; 39 | 40 | static const char MSG_DEFAULT[] = "unknown status"; 41 | static const char MSG_SUCCESS[] = "success"; 42 | static const char MSG_OUT_OF_RESOURCES[] = "out of resources"; 43 | static const char MSG_INVALID_VALUE[] = "invalid value"; 44 | static const char MSG_INVALID_ENVIRONMENT[] = "invalid environment"; 45 | static const char MSG_ABSTRACT_FUNCTION[] = "Abstract function"; 46 | 47 | clprobdistStatus clprobdistSetErrorString(cl_int err, const char* msg, ...) 48 | { 49 | char formatted[1024]; 50 | const char* base; 51 | switch (err) { 52 | CASE_ERR(SUCCESS); 53 | CASE_ERR(OUT_OF_RESOURCES); 54 | CASE_ERR(INVALID_VALUE); 55 | CASE_ERR(INVALID_ENVIRONMENT); 56 | CASE_ERR(ABSTRACT_FUNCTION); 57 | default: base = MSG_DEFAULT; 58 | } 59 | va_list args; 60 | va_start(args, msg); 61 | vsprintf(formatted, msg, args); 62 | sprintf(errorString, "[%s] %s", base, formatted); 63 | va_end(args); 64 | return (clprobdistStatus)err; 65 | } 66 | 67 | void arraycopy(cl_double* src, int index_s, cl_double *dest, int index_d, int size) { 68 | for (int i = 0; i < size; i++) 69 | dest[index_d + i] = src[index_s + i]; 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/library/private.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * Nabil Kemerchou (2015) 20 | * David Munger (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | */ 24 | 25 | #pragma once 26 | #ifndef CLPROBDIST_PRIVATE_H 27 | #define CLPROBDIST_PRIVATE_H 28 | 29 | /*! @brief Set the current error string 30 | * 31 | * The error string will be constructed based on the error code \c err and on 32 | * the optional message \c msg. 33 | * 34 | * @param[in] err Error code. 35 | * @param[in] msg Additional error message (format string). Can be `NULL`. 36 | * @param[in] ... Additional arguments for the format string. 37 | * @return The value of err (for convenience). 38 | */ 39 | clprobdistStatus clprobdistSetErrorString(cl_int err, const char* msg, ...); 40 | 41 | void arraycopy(cl_double* src, int index_s, cl_double *dest, int index_d, int size); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | # *********************************************************************** 3 | # Copyright (c) 2015 Advanced Micro Devices, Inc. 4 | # All rights reserved. 5 | # 6 | # Redistribution and use in source and binary forms, with or without 7 | # modification, are permitted provided that the following conditions 8 | # are met: 9 | # 10 | # 1. Redistributions of source code must retain the above copyright 11 | # notice, this list of conditions and the following disclaimer. 12 | # 13 | # 2. Redistributions in binary form must reproduce the above copyright 14 | # notice, this list of conditions and the following disclaimer in the 15 | # documentation and/or other materials provided with the distribution. 16 | # 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | # 29 | # *********************************************************************** 30 | 31 | set( clProbDist.CTest.Source 32 | ctest/cli.c 33 | ctest/util.c 34 | ctest/exponential_dispatch.c 35 | ctest/exponential_checks.c 36 | ctest/normal_dispatch.c 37 | ctest/normal_checks.c 38 | ctest/lognormal_dispatch.c 39 | ctest/lognormal_checks.c 40 | ctest/gamma_dispatch.c 41 | ctest/gamma_checks.c 42 | ctest/poisson_dispatch.c 43 | ctest/poisson_checks.c 44 | ${PROJECT_SOURCE_DIR}/client/common.c 45 | ) 46 | set( clProbDist.CTest.Headers 47 | ${PROJECT_SOURCE_DIR}/include/clProbDist/clProbDist.h 48 | ${PROJECT_SOURCE_DIR}/include/clProbDist/exponential.h 49 | ${PROJECT_SOURCE_DIR}/include/clProbDist/gamma.h 50 | ${PROJECT_SOURCE_DIR}/include/clProbDist/normal.h 51 | ${PROJECT_SOURCE_DIR}/include/clProbDist/lognormal.h 52 | ${PROJECT_SOURCE_DIR}/include/clProbDist/poisson.h 53 | ${PROJECT_SOURCE_DIR}/client/common.h 54 | ctest/util.h 55 | ctest/mangle.h 56 | ctest/checks.h 57 | ctest/dispatch.c.h 58 | ctest/checks.c.h 59 | ) 60 | 61 | set( clProbDist.CTest.Files ${clProbDist.CTest.Source} ${clProbDist.CTest.Headers} ) 62 | 63 | include_directories( 64 | ${OPENCL_INCLUDE_DIRS} 65 | ${PROJECT_SOURCE_DIR}/include 66 | ) 67 | 68 | set( Client.Source ${clProbDist.CTest.Source} ) 69 | 70 | if( MSVC ) 71 | if( MSVC_VERSION LESS 1800 ) 72 | # Use C++ with Microsoft compiler 73 | SET_SOURCE_FILES_PROPERTIES( ${Client.Source} PROPERTIES LANGUAGE CXX) 74 | endif () 75 | endif( ) 76 | 77 | set( DL_LIB "" ) 78 | if( WIN32 ) 79 | add_definitions( "/D_CONSOLE" ) 80 | elseif( APPLE ) 81 | set( CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ ${CMAKE_CXX_FLAGS}" ) 82 | else( ) 83 | # To use the dlopen() and dlclose() functions, we should link with libdl 84 | set( DL_LIB "-ldl" ) 85 | endif( ) 86 | 87 | if( CMAKE_COMPILER_IS_GNUCC ) 88 | set( MATH_LIB "-lm" ) 89 | endif() 90 | 91 | if( NOT BUILD_RUNTIME ) 92 | message( ERROR "Building tests requires building runtime." ) 93 | endif( ) 94 | 95 | add_executable( CTest ${clProbDist.CTest.Files} ) 96 | 97 | include_directories( CTest ${OPENCL_INCLUDE_DIRS} ${PROJECT_BINARY_DIR}/include ../include ) 98 | target_link_libraries( CTest clProbDist ${OPENCL_LIBRARIES} ${DL_LIB} ${MATH_LIB} ) 99 | 100 | set_target_properties( CTest PROPERTIES VERSION ${CLPROBDIST_VERSION} ) 101 | set_target_properties( CTest PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/staging" ) 102 | 103 | 104 | # CPack configuration; include the executable into the package 105 | install( TARGETS CTest 106 | RUNTIME DESTINATION bin${SUFFIX_BIN} 107 | ) 108 | 109 | -------------------------------------------------------------------------------- /src/tests/ctest/checks.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #ifndef CTEST_ACTIONS_H 55 | #define CTEST_ACTIONS_H 56 | 57 | #include "mangle.h" 58 | #include "util.h" 59 | 60 | int CTEST_MANGLE(checkHost)(); 61 | int CTEST_MANGLE(checkDevice)(const DeviceSelect*); 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /src/tests/ctest/cli.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #include 55 | #include 56 | #include 57 | #include 58 | 59 | #ifdef __APPLE__ 60 | #include 61 | #else 62 | #include 63 | #endif 64 | 65 | #include "util.h" 66 | 67 | // defined in _common.c 68 | int ctestExponential_dispatch(const DeviceSelect* dev); 69 | int ctestNormal_dispatch(const DeviceSelect* dev); 70 | int ctestLognormal_dispatch(const DeviceSelect* dev); 71 | int ctestGamma_dispatch(const DeviceSelect* dev); 72 | int ctestPoisson_dispatch(const DeviceSelect* dev); 73 | 74 | // defined below in this file 75 | int usage(); 76 | 77 | // for use from within usage() 78 | static const char* global_prog; 79 | 80 | 81 | // main program: command line interface (CLI) 82 | int main(int argc, char** argv) 83 | { 84 | global_prog = portable_basename(*argv++); argc--; 85 | 86 | while (argc >= 1 && (*argv)[0] == '-') { 87 | if (strcmp(*argv, "-h") == 0) 88 | return usage(); 89 | else if (strcmp(*argv, "-v") == 0) 90 | ctestVerbose++; 91 | else { 92 | fprintf(stderr, "ERROR: unknown switch `%s'\n", *argv); 93 | return usage(); 94 | } 95 | argv++; argc--; 96 | } 97 | 98 | // parse integer arguments 99 | size_t nargs = 0; 100 | long args[10]; 101 | while (argc > 0 && nargs < (sizeof(args)/sizeof(args[0]))) { 102 | const char* s = *argv++; argc--; 103 | if (strcmp(s, "CPU") == 0) 104 | args[nargs++] = CL_DEVICE_TYPE_CPU; 105 | else if (strcmp(s, "GPU") == 0) 106 | args[nargs++] = CL_DEVICE_TYPE_GPU; 107 | else { 108 | args[nargs++] = strtol(s, (char**) NULL, 10); 109 | if (errno) { 110 | fprintf(stderr, "error interpreting %s as an integer\n", s); 111 | return EXIT_FAILURE; 112 | } 113 | } 114 | } 115 | 116 | const DeviceSelect* dev = parse_device_select(nargs, args); 117 | clinfo(dev); 118 | 119 | // launch tests 120 | int ret = 0; 121 | ret |= ctestExponential_dispatch(dev); 122 | ret |= ctestNormal_dispatch(dev); 123 | ret |= ctestLognormal_dispatch(dev); 124 | ret |= ctestGamma_dispatch(dev); 125 | ret |= ctestPoisson_dispatch(dev); 126 | return ret; 127 | } 128 | 129 | 130 | // display usage directives 131 | int usage() 132 | { 133 | fprintf(stderr, "usage:\n"); 134 | fprintf(stderr, "\n run tests:\n"); 135 | fprintf(stderr, " %s [-v] [-v] [CPU|GPU] [] []\n", global_prog); 136 | fprintf(stderr, "\n display help:\n"); 137 | fprintf(stderr, " %s -h\n", global_prog); 138 | fprintf(stderr, "\nwhere:\n"); 139 | fprintf(stderr, " : device index (starting from 0)\n"); 140 | fprintf(stderr, " : device index (starting from 0)\n"); 141 | fprintf(stderr, " the `-v' switch: enables verbose output (can be used twice)\n"); 142 | return EXIT_FAILURE; 143 | } 144 | -------------------------------------------------------------------------------- /src/tests/ctest/dispatch.c.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #include "mangle.h" 55 | #include "checks.h" 56 | #include "util.h" 57 | 58 | #include 59 | #include 60 | 61 | #include "../client/common.h" 62 | 63 | int CTEST_MANGLE(dispatch)(const DeviceSelect* dev) 64 | { 65 | int ret = 0; 66 | 67 | ret |= CTEST_MANGLE(checkHost)(); 68 | ret |= CTEST_MANGLE(checkDevice)(dev); 69 | 70 | return ret; 71 | } 72 | -------------------------------------------------------------------------------- /src/tests/ctest/exponential_checks.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #define CTEST_PROBDIST_TYPE Exponential 55 | #define CTEST_PROBDIST_TYPE_C EXPONENTIAL 56 | #define CTEST_PROBDIST_HEADER exponential 57 | #define CTEST_PROBDIST_PARAM_COUNT 1 58 | #define CTEST_PROBDIST_PARAM_DEF cl_double p1; 59 | #include "mangle.h" 60 | 61 | typedef struct { CTEST_PROBDIST_PARAM_DEF } DistParams; 62 | 63 | DistParams CTEST_MANGLE(inputDistParams)[] = { 64 | { 1e-9 }, 65 | { 1.0 }, 66 | { 1e9 } 67 | }; 68 | 69 | cl_double CTEST_MANGLE(inputCDFValues)[] = { 70 | 1e-9, 71 | 1e-6, 72 | 1e-3, 73 | 0.01, 74 | 0.1, 75 | 0.5, 76 | 0.9, 77 | 0.99, 78 | 1 - 1e-3, 79 | 1 - 1e-6, 80 | 1 - 1e-9 81 | }; 82 | 83 | MemType CTEST_MANGLE(objMemTypes)[] = { 84 | MEM_PARAMS, 85 | MEM_GLOBAL, 86 | MEM_CONSTANT, 87 | MEM_LOCAL, 88 | MEM_PRIVATE 89 | }; 90 | 91 | // The following values were obtained from SSJ: 92 | cl_double CTEST_MANGLE(expectedInverseCDFValues) 93 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputDistParams))] 94 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputCDFValues))] = { 95 | { 1.00000000050000000, 1000.00050000033330, 1000500.33358353340, 10050335.8535014410, 105360515.657826300, 693147180.559945200, 2302585092.99404570, 4605170185.98809000, 6907755278.98213600, 13815510557.9355160, 20723265865.2283400 }, 96 | { 1.00000000050000010e-09, 1.00000050000033340e-06, 0.00100050033358353350, 0.0100503358535014420, 0.105360515657826310, 0.693147180559945300, 2.30258509299404600, 4.60517018598809000, 6.90775527898213600, 13.8155105579355180, 20.7232658652283420 }, 97 | { 1.00000000050000020e-18, 1.00000050000033350e-15, 1.00050033358353350e-12, 1.00503358535014410e-11, 1.05360515657826310e-10, 6.93147180559945300e-10, 2.30258509299404600e-09, 4.60517018598809000e-09, 6.90775527898213600e-09, 1.38155105579355170e-08, 2.07232658652283400e-08 } 98 | }; 99 | cl_double CTEST_MANGLE(expectedDensityValues) 100 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputDistParams))] 101 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputCDFValues))] = { 102 | { 9.99999999000000100e-10, 9.99999000000000000e-10, 9.99000000000000000e-10, 9.90000000000000000e-10, 9.00000000000000100e-10, 5.00000000000000000e-10, 9.99999999999999800e-11, 1.00000000000000140e-11, 1.00000000000000120e-12, 1.00000000002875620e-15, 9.99999971718069600e-19 }, 103 | { 0.999999999000000000, 0.999999000000000000, 0.999000000000000000, 0.990000000000000000, 0.900000000000000000, 0.500000000000000000, 0.0999999999999999800, 0.0100000000000000140, 0.00100000000000000100, 1.00000000002875600e-06, 9.99999971718069600e-10 }, 104 | { 999999999.000000000, 999999000.000000000, 999000000.000000000, 990000000.000000000, 900000000.000000000, 500000000.000000000, 99999999.9999999900, 10000000.0000000150, 1000000.00000000100, 1000.00000002875610, 0.999999971718069500 } 105 | }; 106 | 107 | #include "checks.c.h" 108 | -------------------------------------------------------------------------------- /src/tests/ctest/exponential_dispatch.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #define CTEST_PROBDIST_TYPE Exponential 55 | #define CTEST_PROBDIST_HEADER exponential 56 | #define CTEST_PROBDIST_PARAM_COUNT 1 57 | #include "dispatch.c.h" 58 | -------------------------------------------------------------------------------- /src/tests/ctest/gamma_checks.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #define CTEST_PROBDIST_TYPE Gamma 55 | #define CTEST_PROBDIST_TYPE_C GAMMA 56 | #define CTEST_PROBDIST_HEADER gamma 57 | #define CTEST_PROBDIST_PARAM_COUNT 3 58 | #define CTEST_PROBDIST_PARAM_DEF cl_double p1; cl_double p2; cl_int p3; 59 | // Gamma can very approximate in some cases 60 | #define CTEST_REL_TOL 1e-12 61 | #include "mangle.h" 62 | 63 | typedef struct { CTEST_PROBDIST_PARAM_DEF } DistParams; 64 | 65 | DistParams CTEST_MANGLE(inputDistParams)[] = { 66 | { 0.1, 1.0, 15 }, 67 | { 1.0, 1.0, 15 }, 68 | { 30.0, 1.0, 15 }, 69 | { 100.0, 1.0, 15 }, 70 | { 5.0, 5.0, 15 } 71 | }; 72 | 73 | cl_double CTEST_MANGLE(inputCDFValues)[] = { 74 | 1e-9, 75 | 1e-6, 76 | 1e-3, 77 | 0.01, 78 | 0.1, 79 | 0.5, 80 | 0.9, 81 | 0.99, 82 | 1 - 1e-3, 83 | 1 - 1e-6, 84 | 1 - 1e-9 85 | }; 86 | 87 | MemType CTEST_MANGLE(objMemTypes)[] = { 88 | MEM_PARAMS, 89 | MEM_GLOBAL, 90 | MEM_CONSTANT, 91 | MEM_LOCAL, 92 | MEM_PRIVATE 93 | }; 94 | 95 | // The following values were obtained from SSJ: 96 | cl_double CTEST_MANGLE(expectedInverseCDFValues) 97 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputDistParams))] 98 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputCDFValues))] = { 99 | { 6.07304836240804300e-91, 6.07304836240802700e-61, 6.07304836240797400e-31, 6.07304836240788850e-21, 6.07304836274321100e-11, 0.000593391104460228000, 0.266154553738837450, 1.58847781792950830, 3.36367701171871670, 9.45702706042514800, 15.9272310103114770 }, 100 | { 1.00000000050000000e-09, 1.00000050000033320e-06, 0.00100050033358353370, 0.0100503358535014420, 0.105360515657826320, 0.693147180559945400, 2.30258509299404550, 4.60517018598808600, 6.90775527898215500, 13.8155105579547830, 20.7232658514492200 }, 101 | { 7.73964772729710600, 10.7108586507588500, 15.8691707971403650, 18.7424257649019000, 23.2294441501017100, 29.6673331382212240, 37.1985028596843500, 44.1897094507246800, 49.8036165349246000, 63.5481801248736700, 75.2403798219337700 }, 102 | { 51.1433022288374300, 59.4363206981229400, 71.9213974950003800, 78.2159830537956800, 87.4176364995933600, 99.6668649193153700, 113.010523859844400, 124.722561490720920, 133.770263911378780, 154.919045994941290, 172.071039903503330 }, 103 | { 0.00831522744855309500, 0.0338126003242958000, 0.147874346383566950, 0.255821216018721150, 0.486518205192533600, 0.934181776559196400, 1.59871791721052530, 2.32092511589543540, 2.95882984450744370, 4.68630468468288800, 6.29454573748513200 } 104 | }; 105 | cl_double CTEST_MANGLE(expectedDensityValues) 106 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputDistParams))] 107 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputCDFValues))] = { 108 | { 1.64661952338464700e+80, 1.64661952338465080e+53, 1.64661952338461900e+26, 164661952338464384, 164661952.320282200, 84.2160157592402200, 0.265124193883214200, 0.0141549759553882010, 0.00122095625051812520, 1.08729024074481670e-06, 1.05347573059169430e-09 }, 109 | { 0.999999998999999900, 0.999998999999999900, 0.998999999999999900, 0.989999999999999900, 0.899999999999999900, 0.499999999999999900, 0.100000000000000020, 0.0100000000000000570, 0.000999999999999982500, 1.00000000000949160e-06, 9.99999985497191300e-10 }, 110 | { 2.91802835722380600e-09, 1.84798620351826280e-06, 0.000949818595408995200, 0.00669387000683508800, 0.0380336043913488700, 0.0733126382223350500, 0.0277712240981310460, 0.00377074743557629200, 0.000441154666040773830, 5.55683670437560900e-07, 6.22383119595044400e-10 }, 111 | { 9.74608632656952200e-10, 7.05131406014296300e-07, 0.000420614187357256640, 0.00314399472415045470, 0.0192002182981725750, 0.0399719958019439700, 0.0161777961096207720, 0.00230267825264469280, 0.000277912249561340730, 3.71437804769975300e-07, 4.32079609448187600e-10 }, 112 | { 5.97143934531665300e-07, 0.000143724211734523270, 0.0297238505355180150, 0.155194491946491130, 0.640581370612566900, 0.928569449561913500, 0.287180072667952260, 0.0344713616948917500, 0.00375060536553297130, 4.18581027409264200e-06, 4.38595281209594800e-09 } 113 | }; 114 | 115 | #include "checks.c.h" 116 | -------------------------------------------------------------------------------- /src/tests/ctest/gamma_dispatch.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #define CTEST_PROBDIST_TYPE Gamma 55 | #define CTEST_PROBDIST_HEADER gamma 56 | #define CTEST_PROBDIST_PARAM_COUNT 3 57 | #include "dispatch.c.h" 58 | -------------------------------------------------------------------------------- /src/tests/ctest/lognormal_checks.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #define CTEST_PROBDIST_TYPE Lognormal 55 | #define CTEST_PROBDIST_TYPE_C LOGNORMAL 56 | #define CTEST_PROBDIST_HEADER lognormal 57 | #define CTEST_PROBDIST_PARAM_COUNT 2 58 | #define CTEST_PROBDIST_PARAM_DEF cl_double p1; cl_double p2; 59 | #define CTEST_REL_TOL 1e-12 60 | #include "mangle.h" 61 | 62 | typedef struct { CTEST_PROBDIST_PARAM_DEF } DistParams; 63 | 64 | DistParams CTEST_MANGLE(inputDistParams)[] = { 65 | { 0.0, 1e-2 }, 66 | { 0.0, 1.0 }, 67 | { 0.0, 30.0 }, 68 | { 5.0, 1.0 } 69 | }; 70 | 71 | cl_double CTEST_MANGLE(inputCDFValues)[] = { 72 | 1e-9, 73 | 1e-6, 74 | 1e-3, 75 | 0.01, 76 | 0.1, 77 | 0.5, 78 | 0.9, 79 | 0.99, 80 | 1 - 1e-3, 81 | 1 - 1e-6, 82 | 1 - 1e-9 83 | }; 84 | 85 | MemType CTEST_MANGLE(objMemTypes)[] = { 86 | MEM_PARAMS, 87 | MEM_GLOBAL, 88 | MEM_CONSTANT, 89 | MEM_LOCAL, 90 | MEM_PRIVATE 91 | }; 92 | 93 | // The following values were obtained from SSJ: 94 | cl_double CTEST_MANGLE(expectedInverseCDFValues) 95 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputDistParams))] 96 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputCDFValues))] = { 97 | { 0.941785186565590700, 0.953577819124149000, 0.969570273106711100, 0.977005029803317400, 0.987266253388433800, 1.00000000000000000, 1.01289798630092150, 1.02353618404739620, 1.03138475646101000, 1.04868211062043300, 1.06181326093335660 }, 98 | { 0.00248419400773548800, 0.00862211979347149000, 0.0454913852476534600, 0.0976517330703360300, 0.277606241852009770, 1.00000000000000000, 3.60222447927915820, 10.2404736563121280, 21.9821839795828600, 115.980759250337750, 402.545051425157850 }, 99 | { 7.17076179336556000e-79, 1.17063519235743760e-62, 5.46858351517757700e-41, 4.90228616987869700e-31, 2.00852297685636020e-17, 1.00000000000000000, 49787829739700072.0, 2.03986459653118140e+30, 1.82862709735452900e+40, 8.54237089704992300e+61, 1.39455216423969660e+78 }, 100 | { 0.368687080511714400, 1.27963603670995900, 6.75152019655660000, 14.4928021968101230, 41.2004193398506800, 148.413159102576600, 534.617514766454000, 1519.82104603999550, 3262.44536838394300, 17213.0708754580260, 59742.9827631168300 } 101 | }; 102 | cl_double CTEST_MANGLE(expectedDensityValues) 103 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputDistParams))] 104 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputCDFValues))] = { 105 | { 6.53688582983104600e-07, 0.000518922799725664200, 0.347276537911490770, 2.72794319276162400, 17.7761907013585620, 39.8942280401432750, 17.3263580642906870, 2.60392769878115000, 0.326463044559375000, 0.000471862032029027840, 5.79795176579755100e-07 }, 106 | { 2.47820509253108420e-06, 0.000573911385493486200, 0.0740159935498482500, 0.272930560118797350, 0.632184387359863200, 0.398942280401432700, 0.0487194323790742160, 0.00260262787620472830, 0.000153173591859268730, 4.26651174615785140e-08, 1.52935480125267500e-11 }, 107 | { 2.86177973749881460e+68, 1.40901644647496250e+55, 2.05238405138424770e+36, 1.81222537675162530e+27, 291256035661867.300, 0.0132980760133810900, 1.17497477362121300e-19, 4.35521427072176800e-34, 6.13773776318336100e-45, 1.93089747422370200e-69, 1.47152188089287930e-88 }, 108 | { 1.66980145663381400e-08, 3.86698449762681800e-06, 0.000498715841623529300, 0.00183899164851116630, 0.00425962489568007360, 0.00268805194103914670, 0.000328268953195730350, 1.75363686882098540e-05, 1.03207554360729950e-06, 2.87475300165870570e-10, 1.03047115936374120e-13 } 109 | }; 110 | 111 | #include "checks.c.h" 112 | -------------------------------------------------------------------------------- /src/tests/ctest/lognormal_dispatch.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #define CTEST_PROBDIST_TYPE Lognormal 55 | #define CTEST_PROBDIST_HEADER lognormal 56 | #define CTEST_PROBDIST_PARAM_COUNT 2 57 | #include "dispatch.c.h" 58 | -------------------------------------------------------------------------------- /src/tests/ctest/mangle.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #pragma once 55 | #ifndef CTEST_COMMON_H 56 | #define CTEST_COMMON_H 57 | 58 | #ifdef __APPLE__ 59 | #include 60 | #else 61 | #include 62 | #endif 63 | 64 | 65 | #ifndef CTEST_PROBDIST_TYPE 66 | #error "CTEST_PROBDIST_TYPE undefined" 67 | #endif 68 | 69 | #ifndef CTEST_PROBDIST_HEADER 70 | #error "CTEST_PROBDIST_HEADER undefined" 71 | #endif 72 | 73 | #ifndef CTEST_PROBDIST_PARAM_COUNT 74 | #error "CTEST_PROBDIST_PARAM_COUNT undefined" 75 | #endif 76 | 77 | #define PROBDIST_HOST_HEADER 78 | #define PROBDIST_DEVICE_HEADER 79 | 80 | #define _PROBDIST_MANGLE(ident) _PROBDIST_MANGLE_(ident,CTEST_PROBDIST_TYPE) 81 | #define _PROBDIST_MANGLE_(ident,PROBDIST) _PROBDIST_MANGLE__(ident,PROBDIST) 82 | #define _PROBDIST_MANGLE__(ident,probdist) clprobdist ## probdist ## ident 83 | 84 | #define CTEST_MANGLE(ident) CTEST_MANGLE_(ident,CTEST_PROBDIST_TYPE) 85 | #define CTEST_MANGLE_(ident,probdist) CTEST_MANGLE__(ident,probdist) 86 | #define CTEST_MANGLE__(ident,probdist) ctest ## probdist ## _ ## ident 87 | 88 | // utility macros 89 | #define CTEST_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 90 | 91 | #if CTEST_PROBDIST_PARAM_COUNT == 1 92 | #define CTEST_DIST_PARAMS(params) (params).p1 93 | #elif CTEST_PROBDIST_PARAM_COUNT == 2 94 | #define CTEST_DIST_PARAMS(params) (params).p1, (params).p2 95 | #elif CTEST_PROBDIST_PARAM_COUNT == 3 96 | #define CTEST_DIST_PARAMS(params) (params).p1, (params).p2, (params).p3 97 | #else 98 | #error "unsupported value of CTEST_PROBDIST_PARAM_COUNT" 99 | #endif 100 | 101 | // for size types 102 | #if defined(_MSC_VER) || defined(__MINGW32__) 103 | #define SIZE_T_FORMAT "Iu" 104 | #elif defined(__GNUC__) 105 | #define SIZE_T_FORMAT "zu" 106 | #else 107 | #define SIZE_T_FORMAT "lu" 108 | #endif 109 | 110 | #define _CTEST_STR_(x) _CTEST_STR__(x) 111 | #define _CTEST_STR__(x) #x 112 | 113 | #define PROBDIST_TYPE_S _CTEST_STR_(CTEST_PROBDIST_TYPE) 114 | #define PROBDIST_TYPE_SC _CTEST_STR_(CTEST_PROBDIST_TYPE_C) 115 | #define PROBDIST_DEVICE_HEADER_S _CTEST_STR_(PROBDIST_DEVICE_HEADER) 116 | 117 | #define clprobdistObject _PROBDIST_MANGLE() 118 | #define clprobdistCreate _PROBDIST_MANGLE(Create) 119 | #define clprobdistDestroy _PROBDIST_MANGLE(Destroy) 120 | #define clprobdistDensityWithObject _PROBDIST_MANGLE(DensityWithObject) 121 | #define clprobdistProbWithObject _PROBDIST_MANGLE(ProbWithObject) 122 | #define clprobdistCDFWithObject _PROBDIST_MANGLE(CDFWithObject) 123 | #define clprobdistComplCDFWithObject _PROBDIST_MANGLE(ComplCDFWithObject) 124 | #define clprobdistInverseCDFWithObject _PROBDIST_MANGLE(InverseCDFWithObject) 125 | #define clprobdistMeanWithObject _PROBDIST_MANGLE(MeanWithObject) 126 | #define clprobdistVarianceWithObject _PROBDIST_MANGLE(VarianceWithObject) 127 | #define clprobdistStdDeviationWithObject _PROBDIST_MANGLE(StdDeviationWithObject) 128 | #define clprobdistDensity _PROBDIST_MANGLE(Density) 129 | #define clprobdistProb _PROBDIST_MANGLE(Prob) 130 | #define clprobdistCDF _PROBDIST_MANGLE(CDF) 131 | #define clprobdistComplCDF _PROBDIST_MANGLE(ComplCDF) 132 | #define clprobdistInverseCDF _PROBDIST_MANGLE(InverseCDF) 133 | #define clprobdistMean _PROBDIST_MANGLE(Mean) 134 | #define clprobdistVariance _PROBDIST_MANGLE(Variance) 135 | #define clprobdistStdDeviation _PROBDIST_MANGLE(StdDeviation) 136 | 137 | #include PROBDIST_HOST_HEADER 138 | 139 | // defined in util.c 140 | extern int ctestVerbose; 141 | 142 | /*! @brief Enumeration for memory types 143 | */ 144 | typedef enum MemType_ { MEM_PARAMS, MEM_PRIVATE, MEM_LOCAL, MEM_CONSTANT, MEM_GLOBAL } MemType; 145 | 146 | #endif 147 | -------------------------------------------------------------------------------- /src/tests/ctest/normal_checks.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #define CTEST_PROBDIST_TYPE Normal 55 | #define CTEST_PROBDIST_TYPE_C NORMAL 56 | #define CTEST_PROBDIST_HEADER normal 57 | #define CTEST_PROBDIST_PARAM_COUNT 2 58 | #define CTEST_PROBDIST_PARAM_DEF cl_double p1; cl_double p2; 59 | #include "mangle.h" 60 | 61 | typedef struct { CTEST_PROBDIST_PARAM_DEF } DistParams; 62 | 63 | DistParams CTEST_MANGLE(inputDistParams)[] = { 64 | { 0.0, 1e-9 }, 65 | { 0.0, 1.0 }, 66 | { 0.0, 1e9 }, 67 | { 5.0, 1.0 } 68 | }; 69 | 70 | cl_double CTEST_MANGLE(inputCDFValues)[] = { 71 | 1e-9, 72 | 1e-6, 73 | 1e-3, 74 | 0.01, 75 | 0.1, 76 | 0.5, 77 | 0.9, 78 | 0.99, 79 | 1 - 1e-3, 80 | 1 - 1e-6, 81 | 1 - 1e-9 82 | }; 83 | 84 | MemType CTEST_MANGLE(objMemTypes)[] = { 85 | MEM_PARAMS, 86 | MEM_GLOBAL, 87 | MEM_CONSTANT, 88 | MEM_LOCAL, 89 | MEM_PRIVATE 90 | }; 91 | 92 | // The following values were obtained from SSJ: 93 | cl_double CTEST_MANGLE(expectedInverseCDFValues) 94 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputDistParams))] 95 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputCDFValues))] = { 96 | { -5.99780701500768700e-09, -4.75342430882289900e-09, -3.09023230616781460e-09, -2.32634787404084030e-09, -1.28155156554460060e-09, 0.00000000000000000, 1.28155156554460060e-09, 2.32634787404084030e-09, 3.09023230616781460e-09, 4.75342430881708800e-09, 5.99780701960163800e-09 }, 97 | { -5.99780701500768650, -4.75342430882289900, -3.09023230616781450, -2.32634787404084030, -1.28155156554460060, 0.00000000000000000, 1.28155156554460060, 2.32634787404084030, 3.09023230616781450, 4.75342430881708700, 5.99780701960163750 }, 98 | { -5997807015.00768700, -4753424308.82289900, -3090232306.16781470, -2326347874.04084000, -1281551565.54460050, 0.00000000000000000, 1281551565.54460050, 2326347874.04084000, 3090232306.16781470, 4753424308.81708700, 5997807019.60163800 }, 99 | { -0.997807015007686500, 0.246575691177101320, 1.90976769383218550, 2.67365212595915970, 3.71844843445539900, 5.00000000000000000, 6.28155156554460100, 7.32634787404084000, 8.09023230616781500, 9.75342430881708800, 10.9978070196016380 } 100 | }; 101 | cl_double CTEST_MANGLE(expectedDensityValues) 102 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputDistParams))] 103 | [CTEST_ARRAY_SIZE(CTEST_MANGLE(inputCDFValues))] = { 104 | { 6.15634224080528800, 4948.33271656203200, 3367090.07706398000, 26652142.2034580970, 175498331.932486740, 398942280.401432630, 175498331.932486740, 26652142.2034580970, 3367090.07706398000, 4948.33271669871700, 6.15634207117569950 }, 105 | { 6.15634224080528900e-09, 4.94833271656203300e-06, 0.00336709007706398030, 0.0266521422034580980, 0.175498331932486760, 0.398942280401432650, 0.175498331932486760, 0.0266521422034580980, 0.00336709007706398030, 4.94833271669871800e-06, 6.15634207117570000e-09 }, 106 | { 6.15634224080528900e-18, 4.94833271656203300e-15, 3.36709007706398040e-12, 2.66521422034581000e-11, 1.75498331932486770e-10, 3.98942280401432660e-10, 1.75498331932486770e-10, 2.66521422034581000e-11, 3.36709007706398040e-12, 4.94833271669871800e-15, 6.15634207117570050e-18 }, 107 | { 6.15634224080528900e-09, 4.94833271656203300e-06, 0.00336709007706398030, 0.0266521422034580980, 0.175498331932486700, 0.398942280401432650, 0.175498331932486700, 0.0266521422034581200, 0.00336709007706397400, 4.94833271669870000e-06, 6.15634207117570000e-09 } 108 | }; 109 | 110 | #include "checks.c.h" 111 | -------------------------------------------------------------------------------- /src/tests/ctest/normal_dispatch.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #define CTEST_PROBDIST_TYPE Normal 55 | #define CTEST_PROBDIST_HEADER normal 56 | #define CTEST_PROBDIST_PARAM_COUNT 2 57 | #include "dispatch.c.h" 58 | -------------------------------------------------------------------------------- /src/tests/ctest/poisson_dispatch.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #define CTEST_PROBDIST_TYPE Poisson 55 | #define CTEST_PROBDIST_HEADER poisson 56 | #define CTEST_PROBDIST_PARAM_COUNT 1 57 | #include "dispatch.c.h" 58 | -------------------------------------------------------------------------------- /src/tests/ctest/util.c: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #include "util.h" 55 | 56 | #include 57 | #include 58 | #include 59 | 60 | #ifdef __APPLE__ 61 | #include 62 | #else 63 | #include 64 | #endif 65 | 66 | #include "../client/common.h" 67 | 68 | 69 | // globals 70 | int ctestVerbose = 0; 71 | 72 | // device select 73 | static DeviceSelect device_select; 74 | const DeviceSelect* parse_device_select(size_t nargs, const long* args) 75 | { 76 | device_select.platform_index = 0; 77 | device_select.device_type = CL_DEVICE_TYPE_CPU; 78 | device_select.device_index = 0; 79 | if (nargs > 0) { device_select.device_type = (cl_device_type) *args++; nargs--; } 80 | if (nargs > 0) { device_select.device_index = (size_t) *args++; nargs--; } 81 | if (nargs > 0) { device_select.platform_index = (size_t) *args++; nargs--; } 82 | return &device_select; 83 | } 84 | 85 | 86 | // write_array 87 | 88 | #define IMPLEMENT_WRITE_ARRAY(type) \ 89 | void write_array_##type(FILE* file, size_t num_cols, const char* format, size_t array_size, const type* array) \ 90 | { \ 91 | size_t num_rows = (array_size + num_cols - 1) / num_cols; \ 92 | for (size_t row = 0; row < num_rows; row++) { \ 93 | for (size_t col = 0; col < num_cols; col++) { \ 94 | size_t i = col * num_rows + row; \ 95 | if (i < array_size) \ 96 | fprintf(file, format, array[i]); \ 97 | } \ 98 | fprintf(file, "\n"); \ 99 | } \ 100 | } 101 | 102 | IMPLEMENT_WRITE_ARRAY(cl_float) 103 | IMPLEMENT_WRITE_ARRAY(cl_double) 104 | 105 | 106 | char *portable_basename(char *path) 107 | { 108 | #ifdef _MSC_VER 109 | char* p = strrchr(path, '\\'); 110 | #else 111 | char* p = strrchr(path, '/'); 112 | #endif 113 | return p ? p + 1 : path; 114 | } 115 | 116 | 117 | static int clinfo_helper(cl_context context, cl_device_id device, cl_command_queue queue, void* data_) 118 | { 119 | // Nothing to do; the call_with_opencl() wrapper will output platform and device 120 | // information. 121 | return EXIT_SUCCESS; 122 | } 123 | 124 | int clinfo(const DeviceSelect* dev) 125 | { 126 | return call_with_opencl(dev->platform_index, dev->device_type, dev->device_index, &clinfo_helper, NULL, true); 127 | } 128 | -------------------------------------------------------------------------------- /src/tests/ctest/util.h: -------------------------------------------------------------------------------- 1 | /* This file is part of clProbDist. 2 | * 3 | * Copyright 2015-2016 Pierre L'Ecuyer, Universite de Montreal and Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * Authors: 18 | * 19 | * David Munger (2015) 20 | * Nabil Kemerchou (2015) 21 | * Pierre L'Ecuyer (2015) 22 | * 23 | * 24 | *********************************************************************** 25 | Copyright (c) 2015 Advanced Micro Devices, Inc. 26 | All rights reserved. 27 | 28 | Redistribution and use in source and binary forms, with or without 29 | modification, are permitted provided that the following conditions 30 | are met: 31 | 32 | 1. Redistributions of source code must retain the above copyright 33 | notice, this list of conditions and the following disclaimer. 34 | 35 | 2. Redistributions in binary form must reproduce the above copyright 36 | notice, this list of conditions and the following disclaimer in the 37 | documentation and/or other materials provided with the distribution. 38 | 39 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 40 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 41 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 42 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 43 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 45 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 46 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 47 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 48 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 49 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | 51 | *********************************************************************** 52 | */ 53 | 54 | #ifndef CTEST_UTIL_H 55 | #define CTEST_UTIL_H 56 | 57 | #include 58 | 59 | #ifdef __APPLE__ 60 | #include 61 | #else 62 | #include 63 | #endif 64 | 65 | 66 | 67 | // globals 68 | extern int ctestVerbose; 69 | 70 | typedef struct DeviceSelect_ { 71 | size_t platform_index; 72 | cl_device_type device_type; 73 | size_t device_index; 74 | } DeviceSelect; 75 | 76 | const DeviceSelect* parse_device_select(size_t nargs, const long* args); 77 | 78 | void write_array_cl_float (FILE* file, size_t num_cols, const char* format, size_t array_size, const cl_float* array); 79 | void write_array_cl_double(FILE* file, size_t num_cols, const char* format, size_t array_size, const cl_double* array); 80 | 81 | char *portable_basename(char *path); 82 | 83 | int clinfo(const DeviceSelect* dev); 84 | 85 | #endif 86 | --------------------------------------------------------------------------------