Surface memory surface memory does not work

Hi this is my code. after calling the function “test” I print the array dtr1. I am expecting to get 100 for all the elements but I am not getting it. why is that?

#include “ImageUtil2D.h”
#define W 10
#define H 10
#define MAX 100000
#define No_THREADS 10
surface<void,2> surfD;

global void test()
{
for(int i=0;i<W;i++)
for(int j=0;j<H;j++)
{
float a=100;
surf2Dwrite(a, surfD, i,j, cudaBoundaryModeTrap);
}
}

int main()
{
int image = new int[WH];
float dtr = new float[WH];
ImageUtil2D::InitImg(image, dtr, W, H);
const size_t sizef = size_t(W*H)*sizeof(float);

cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);
cudaArray* cuArrD;
cudaMallocArray(&cuArrD, &channelDesc, W*H, 0, cudaArraySurfaceLoadStore);
//cudaMemcpyToArray(cuArrD, 0, 0, dtr, sizef, cudaMemcpyHostToDevice);
cudaBindSurfaceToArray(surfD, cuArrD);
	
test<<<1, 1>>>();

float *dtr1=new float[W*H];
cudaMemcpyFromArray(&dtr1, cuArrD, 0, 0, sizef, cudaMemcpyDeviceToHost );
ImageUtil2D::Print(dtr1);
return 0;

}

Hi this is my code. after calling the function “test” I print the array dtr1. I am expecting to get 100 for all the elements but I am not getting it. why is that?

#include “ImageUtil2D.h”
#define W 10
#define H 10
#define MAX 100000
#define No_THREADS 10
surface<void,2> surfD;

global void test()
{
for(int i=0;i<W;i++)
for(int j=0;j<H;j++)
{
float a=100;
surf2Dwrite(a, surfD, i,j, cudaBoundaryModeTrap);
}
}

int main()
{
int image = new int[WH];
float dtr = new float[WH];
ImageUtil2D::InitImg(image, dtr, W, H);
const size_t sizef = size_t(W*H)*sizeof(float);

cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);
cudaArray* cuArrD;
cudaMallocArray(&cuArrD, &channelDesc, W*H, 0, cudaArraySurfaceLoadStore);
//cudaMemcpyToArray(cuArrD, 0, 0, dtr, sizef, cudaMemcpyHostToDevice);
cudaBindSurfaceToArray(surfD, cuArrD);
	
test<<<1, 1>>>();

float *dtr1=new float[W*H];
cudaMemcpyFromArray(&dtr1, cuArrD, 0, 0, sizef, cudaMemcpyDeviceToHost );
ImageUtil2D::Print(dtr1);
return 0;

}

Hi,

could it be that surf is of type void and when you access it with:

[font=arial, verdana, tahoma, sans-serif]surf2Dwrite(a, surfD, i,j, cudaBoundaryModeTrap);[/font]

[font=“arial, verdana, tahoma, sans-serif”]you should in fact be using:[/font]

[font=“arial, verdana, tahoma, sans-serif”]surf2Dwrite(a, surfD, 4*i,j, cudaBoundaryModeTrap);[/font]

[font=“arial, verdana, tahoma, sans-serif”]

[/font]

[font=“arial, verdana, tahoma, sans-serif”]Also you need to specify allocation of 2D array:[/font]

[font=“arial, verdana, tahoma, sans-serif”]cudaMallocArray(&cuArrD, &channelDesc, W, H, cudaArraySurfaceLoadStore);[/font]

Hi,

could it be that surf is of type void and when you access it with:

[font=arial, verdana, tahoma, sans-serif]surf2Dwrite(a, surfD, i,j, cudaBoundaryModeTrap);[/font]

[font=“arial, verdana, tahoma, sans-serif”]you should in fact be using:[/font]

[font=“arial, verdana, tahoma, sans-serif”]surf2Dwrite(a, surfD, 4*i,j, cudaBoundaryModeTrap);[/font]

[font=“arial, verdana, tahoma, sans-serif”]

[/font]

[font=“arial, verdana, tahoma, sans-serif”]Also you need to specify allocation of 2D array:[/font]

[font=“arial, verdana, tahoma, sans-serif”]cudaMallocArray(&cuArrD, &channelDesc, W, H, cudaArraySurfaceLoadStore);[/font]

Hi Thank you for your reply. But it doesnt work…

Hi Thank you for your reply. But it doesnt work…

Hi,

It works… the changes should be…

cudaMemcpyFromArray(&dtr1, cuArrD, 0, 0, sizef, cudaMemcpyDeviceToHost );

to

cudaMemcpyFromArray(&dtr1[0], cuArrD, 0, 0, sizef, cudaMemcpyDeviceToHost );

and the changes suggested by Brano…

Hi,

It works… the changes should be…

cudaMemcpyFromArray(&dtr1, cuArrD, 0, 0, sizef, cudaMemcpyDeviceToHost );

to

cudaMemcpyFromArray(&dtr1[0], cuArrD, 0, 0, sizef, cudaMemcpyDeviceToHost );

and the changes suggested by Brano…

Hi,

just a small notice:

&dtr1[0] = dtr1

It is the same thing. :)

Hi,

just a small notice:

&dtr1[0] = dtr1

It is the same thing. :)