paolodelia99 / spread-option-pricing

Pricing spread option where the underlying as following a GBM using the margrabe formula and MC methods.
0 stars 0 forks source link

MC Engine using CUDA #2

Open paolodelia99 opened 4 months ago

paolodelia99 commented 4 months ago

Implement an MC engine for pricing a European spread option leveraging CUDA.

Details of the implementation

Don't know, I have to learn Cuda a little bit.

paolodelia99 commented 4 months ago

This is the documentation

paolodelia99 commented 4 months ago

Example file structure

mylibrary/
├── include/
│   └── mylibrary.h
├── src/
│   ├── mylibrary.cpp
│   └── mylibrary_cuda.cu
├── CMakeLists.txt
└── main.cpp

Implementing the Files

Header File (mylibrary.h)

#ifndef MYLIBRARY_H
#define MYLIBRARY_H

void runParallelOperation(float *data, int size);

#endif // MYLIBRARY_H

C++ Source file (mylibrary.cpp)

#include "mylibrary.h"
#include <cuda_runtime.h>
#include <iostream>

void runParallelOperation(float *data, int size) {
    float *deviceArray;

    // Allocate memory on the device
    cudaMalloc((void**)&deviceArray, size * sizeof(float));

    // Copy data from host to device
    cudaMemcpy(deviceArray, data, size * sizeof(float), cudaMemcpyHostToDevice);

    // Define grid and block size
    int threadsPerBlock = 256;
    int numBlocks = (size + threadsPerBlock - 1) / threadsPerBlock;

    // Launch the CUDA kernel
    myKernel<<<numBlocks, threadsPerBlock>>>(deviceArray, size);

    // Copy results from device to host
    cudaMemcpy(data, deviceArray, size * sizeof(float), cudaMemcpyDeviceToHost);

    // Free device memory
    cudaFree(deviceArray);
}

extern "C" void myKernel(float *d_array, int size);  // Declaration of CUDA kernel

CUDA source File (mylibrary_cuda.cu)

#include <cuda_runtime.h>
#include <iostream>

__global__ void myKernel(float *d_array, int size) {
    int idx = threadIdx.x + blockIdx.x * blockDim.x;
    if (idx < size) {
        d_array[idx] = d_array[idx] * 2.0f;  // Example operation
    }
}
paolodelia99 commented 4 months ago

Don't know Cuda to the degree that I'm able alone to implement this feature. Put this ticket on hold for the time being, and implement the CPP code for the code simulations.

Found some interesting resources: