Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "cuda_runtime.h"
- #include "device_launch_parameters.h"
- #include <stdio.h>
- //changed into global variables
- const int arraySizeX = 12;
- const int arraySizeY = 12;
- const int numThreadsPerBlock = 5;
- //dividing into correct number of blocks (safe)
- int numBlocks = arraySizeX * arraySizeY / numThreadsPerBlock + ((arraySizeX * arraySizeY) % numThreadsPerBlock == 0 ? 0 : 1);
- cudaError_t addWithCuda(int **c, int **a, int **b, size_t sizeX, size_t sizeY);
- __global__ void addKernel(int **c, int **a, int **b)
- {
- int i = threadIdx.x + blockIdx.x*numThreadsPerBlock; //offset for number of blocks
- int j = threadIdx.y + blockIdx.y*numThreadsPerBlock; //offset for number of blocks
- c[i][j] = a[i][j] + b[i][j];
- }
- int main()
- {
- int **a;
- int **b;
- int **c;
- //int a[arraySizeX][arraySizeY];
- //int b[arraySizeX][arraySizeY];
- //int c[arraySizeX][arraySizeY];
- int i;
- a = (int**)malloc(arraySizeX * sizeof(int*));
- for(i = 0; i < arraySizeX; i++)
- a[i] = (int*)malloc(arraySizeY * sizeof(int));
- b = (int**)malloc(arraySizeX * sizeof(int*));
- for(i = 0; i < arraySizeX; i++)
- b[i] = (int*)malloc(arraySizeY * sizeof(int));
- c = (int**)malloc(arraySizeX * sizeof(int*));
- for(i = 0; i < arraySizeX; i++)
- c[i] = (int*)malloc(arraySizeY * sizeof(int));
- //init arrays
- int j;
- int iterator = 1;
- for(i = 0; i < arraySizeX; i++)
- for(j = 0; j < arraySizeY; j++)
- {
- a[i][j] = iterator;
- b[i][j] = iterator * 10;
- c[i][j] = 0;
- iterator++;
- }
- // Add vectors in parallel.
- cudaError_t cudaStatus = addWithCuda(c, a, b, arraySizeX, arraySizeY);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "addWithCuda failed!");
- return 1;
- }
- for(i = 0; i < arraySizeX; i++)
- {
- for(j = 0; j < arraySizeY; j++)
- {
- printf("%d ", c[i][j]);
- }
- printf("\n");
- }
- //printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d}\n",
- // c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10], c[11]);
- system("pause");
- // cudaThreadExit must be called before exiting in order for profiling and
- // tracing tools such as Parallel Nsight and Visual Profiler to show complete traces.
- cudaStatus = cudaThreadExit();
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaThreadExit failed!");
- system("pause");
- return 1;
- }
- return 0;
- }
- // Helper function for using CUDA to add vectors in parallel.
- cudaError_t addWithCuda(int **c, int **a, int **b, size_t sizeX, size_t sizeY)
- {
- int **dev_a = 0;
- int **dev_b = 0;
- int **dev_c = 0;
- cudaError_t cudaStatus;
- int i;
- cudaMalloc((void**)dev_a, sizeX * sizeof(int*));
- for(i = 0; i < sizeX; i++)
- cudaMalloc((void**)&dev_a[i], sizeY * sizeof(int));
- cudaMalloc((void**)dev_b, sizeX * sizeof(int*));
- for(i = 0; i < sizeX; i++)
- cudaMalloc((void**)&dev_b[i], sizeY * sizeof(int));
- cudaMalloc((void**)dev_c, sizeX * sizeof(int*));
- for(i = 0; i < sizeX; i++)
- cudaMalloc((void**)&dev_c[i], sizeY * sizeof(int));
- //dev_b = (int**)malloc(arraySizeX * sizeof(int*));
- //for(i = 0; i < arraySizeX; i++)
- // dev_b[i] = (int*)malloc(arraySizeY * sizeof(int));
- //dev_c = (int**)malloc(arraySizeX * sizeof(int*));
- //for(i = 0; i < arraySizeX; i++)
- // dev_c[i] = (int*)malloc(arraySizeY * sizeof(int));
- // Choose which GPU to run on, change this on a multi-GPU system.
- cudaStatus = cudaSetDevice(0);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
- system("pause");
- goto Error;
- }
- // Allocate GPU buffers for three vectors (two input, one output) .
- // cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int*));
- // if (cudaStatus != cudaSuccess) {
- // fprintf(stderr, "cudaMalloc failed!");
- //system("pause");
- // goto Error;
- // }
- // cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int));
- // if (cudaStatus != cudaSuccess) {
- // fprintf(stderr, "cudaMalloc failed!");
- //system("pause");
- // goto Error;
- // }
- // cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));
- // if (cudaStatus != cudaSuccess) {
- // fprintf(stderr, "cudaMalloc failed!");
- //system("pause");
- // goto Error;
- // }
- // Copy input vectors from host memory to GPU buffers.
- cudaStatus = cudaMemcpy(dev_a, a, sizeX * sizeY * sizeof(int), cudaMemcpyHostToDevice);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaMemcpy failed!");
- system("pause");
- goto Error;
- }
- cudaStatus = cudaMemcpy(dev_b, b, sizeX * sizeY * sizeof(int), cudaMemcpyHostToDevice);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaMemcpy failed!");
- system("pause");
- goto Error;
- }
- // Launch a kernel on the GPU with one thread for each element.
- addKernel<<<numBlocks, numThreadsPerBlock>>>(dev_c, dev_a, dev_b);
- // cudaThreadSynchronize waits for the kernel to finish, and returns
- // any errors encountered during the launch.
- cudaStatus = cudaThreadSynchronize();
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaThreadSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
- system("pause");
- goto Error;
- }
- // Copy output vector from GPU buffer to host memory.
- cudaStatus = cudaMemcpy(c, dev_c, sizeX * sizeY * sizeof(int), cudaMemcpyDeviceToHost);
- if (cudaStatus != cudaSuccess) {
- fprintf(stderr, "cudaMemcpy failed!");
- system("pause");
- goto Error;
- }
- Error:
- cudaFree(dev_c);
- cudaFree(dev_a);
- cudaFree(dev_b);
- return cudaStatus;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement