Skip to content

Commit

Permalink
Add missing print statements in function to hand to Clad
Browse files Browse the repository at this point in the history
  • Loading branch information
kchristin22 committed Nov 22, 2024
1 parent b085a5f commit 4d19839
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions demos/CUDA/BlackScholes/BlackScholes.cu
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@
* DISCLAIMER: The following file has been slightly modified to ensure
* compatibility with Clad and to serve as a Clad demo. Specifically, parts of
* the original `main` function have been moved to a separate function to use
* `clad::gradient` on. Furthermore, Clad cannot clone printf statements, so
* some original print statements have been omitted. The same applies to the
* checkCudaErrors function.
* New helper functions are included in another file and invoked here to verify
* the gradient's results. The original file is available in NVIDIA's
* cuda-samples repository on GitHub.
* `clad::gradient` on. Furthermore, Clad cannot clone checkCudaErrors
* successfully, so these calls have been omitted. The same applies to the
* cudaDeviceSynchronize function. New helper functions are included in another
* file and invoked here to verify the gradient's results. The original file is
* available in NVIDIA's cuda-samples repository on GitHub.
*
* Relevant documentation regarding the problem at hand can be found in NVIDIA's
* cuda-samples repository. Using Clad, we compute some of the Greeks
Expand Down Expand Up @@ -110,16 +109,22 @@ void launch(float* h_CallResultCPU, float* h_CallResultGPU,
*d_StockPrice = nullptr, *d_OptionStrike = nullptr,
*d_OptionYears = nullptr;

printf("...allocating GPU memory for options.\n");
cudaMalloc((void**)&d_CallResult, OPT_SZ);
cudaMalloc((void**)&d_PutResult, OPT_SZ);
cudaMalloc((void**)&d_StockPrice, OPT_SZ);
cudaMalloc((void**)&d_OptionStrike, OPT_SZ);
cudaMalloc((void**)&d_OptionYears, OPT_SZ);

// Copy options data to GPU memory for further processing
printf("...copying input data to GPU mem.\n");
cudaMemcpy(d_StockPrice, h_StockPrice, OPT_SZ, cudaMemcpyHostToDevice);
cudaMemcpy(d_OptionStrike, h_OptionStrike, OPT_SZ, cudaMemcpyHostToDevice);
cudaMemcpy(d_OptionYears, h_OptionYears, OPT_SZ, cudaMemcpyHostToDevice);
printf("Data init done.\n\n");

printf("Executing Black-Scholes GPU kernel (%i iterations)...\n",
NUM_ITERATIONS);

BlackScholesGPU<<<DIV_UP((OPT_N / 2), 128), 128 /*480, 128*/>>>(
(float2*)d_CallResult, (float2*)d_PutResult, (float2*)d_StockPrice,
Expand All @@ -128,14 +133,19 @@ void launch(float* h_CallResultCPU, float* h_CallResultGPU,

// Both call and put is calculated

printf("\nReading back GPU results...\n");
// Read back GPU results to compare them to CPU results
cudaMemcpy(h_CallResultGPU, d_CallResult, OPT_SZ, cudaMemcpyDeviceToHost);
cudaMemcpy(h_PutResultGPU, d_PutResult, OPT_SZ, cudaMemcpyDeviceToHost);

// Calculate options values on CPU
printf("Checking the results...\n");
printf("...running CPU calculations.\n\n");
// Calculate options values on CPU
BlackScholesCPU(h_CallResultCPU, h_PutResultCPU, h_StockPrice, h_OptionStrike,
h_OptionYears, RISKFREE, VOLATILITY, OPT_N);

printf("...releasing GPU memory.\n");
cudaFree(d_OptionYears);
cudaFree(d_OptionStrike);
cudaFree(d_StockPrice);
Expand Down

0 comments on commit 4d19839

Please sign in to comment.