Skip to content

Commit

Permalink
Create SurfaceMemory.cu
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeOwlSolutions committed Sep 1, 2015
1 parent 006fdc0 commit f9abfd6
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions SurfaceMemory.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>

#include "TimingGPU.cuh"
#include "Utilities.cuh"

surface<void, cudaSurfaceType1D> surfD;

/*******************/
/* KERNEL FUNCTION */
/*******************/
__global__ void SurfaceMemoryWrite(const int N) {

int tid = blockIdx.x * blockDim.x + threadIdx.x;

surf1Dwrite((float)tid, surfD, tid * sizeof(float), cudaBoundaryModeTrap);
}

/********/
/* MAIN */
/********/
int main() {

const int N = 10;

cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float>();
//Alternatively
//cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);

cudaArray *d_arr; gpuErrchk(cudaMallocArray(&d_arr, &channelDesc, N, 1, cudaArraySurfaceLoadStore));
gpuErrchk(cudaBindSurfaceToArray(surfD, d_arr));

SurfaceMemoryWrite<<<1, N>>>(N);

float *h_arr = new float[N];
gpuErrchk(cudaMemcpyFromArray(h_arr, d_arr, 0, 0, N * sizeof(float), cudaMemcpyDeviceToHost));

for (int i=0; i<N; i++) printf("h_arr[%i] = %f\n", i, h_arr[i]);

return 0;
}

0 comments on commit f9abfd6

Please sign in to comment.