Files
rocm-systems/tests/src/surface/hipSurfaceObj2D.cpp
T

109 regels
3.3 KiB
C++

2018-03-04 22:49:23 +05:30
/* HIT_START
* BUILD: %t %s ../test_common.cpp EXCLUDE_HIP_PLATFORM vdi
* TEST: %t
2018-03-04 22:49:23 +05:30
* HIT_END
*/
#include <stdio.h>
#include <hip/hip_runtime.h>
#include "test_common.h"
2018-03-12 11:29:03 +05:30
__global__ void tex2DKernel(hipSurfaceObject_t surfaceObject, hipSurfaceObject_t outputSurfObj,
int width, int height) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
2018-03-04 22:49:23 +05:30
float data;
2018-03-12 11:29:03 +05:30
surf2Dread(&data, surfaceObject, x * 4, y, hipBoundaryModeZero);
surf2Dwrite(data, outputSurfObj, x * 4, y, hipBoundaryModeZero);
2018-03-04 22:49:23 +05:30
}
2018-06-02 10:58:03 +05:30
int runTest(int argc, char** argv);
2018-03-04 22:49:23 +05:30
2018-03-12 11:29:03 +05:30
int main(int argc, char** argv) {
2018-06-02 10:58:03 +05:30
int testResult = runTest(argc, argv);
2018-03-04 22:49:23 +05:30
2018-03-12 11:29:03 +05:30
if (testResult) {
2018-03-04 22:49:23 +05:30
passed();
} else {
exit(EXIT_FAILURE);
}
}
2018-06-02 10:58:03 +05:30
int runTest(int argc, char** argv) {
int testResult = 1;
2018-03-04 22:49:23 +05:30
unsigned int width = 256;
unsigned int height = 256;
unsigned int size = width * height * sizeof(float);
2018-03-12 11:29:03 +05:30
float* hData = (float*)malloc(size);
2018-03-04 22:49:23 +05:30
memset(hData, 0, size);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
2018-03-12 11:29:03 +05:30
hData[i * width + j] = i * width + j;
2018-03-04 22:49:23 +05:30
}
}
printf("hData: ");
for (int i = 0; i < 64; i++) {
printf("%f ", hData[i]);
}
printf("\n");
hipChannelFormatDesc channelDesc = hipCreateChannelDesc(32, 0, 0, 0, hipChannelFormatKindFloat);
hipArray *hipArray, *hipOutArray;
hipMallocArray(&hipArray, &channelDesc, width, height);
hipMemcpyToArray(hipArray, 0, 0, hData, size, hipMemcpyHostToDevice);
2019-11-17 22:49:22 -08:00
hipResourceDesc resDesc;
2018-03-04 22:49:23 +05:30
memset(&resDesc, 0, sizeof(resDesc));
resDesc.resType = hipResourceTypeArray;
resDesc.res.array.array = hipArray;
// Create surface object
hipSurfaceObject_t surfaceObject = 0;
hipCreateSurfaceObject(&surfaceObject, &resDesc);
hipMallocArray(&hipOutArray, &channelDesc, width, height);
2019-11-17 22:49:22 -08:00
hipResourceDesc resOutDesc;
2018-03-04 22:49:23 +05:30
memset(&resOutDesc, 0, sizeof(resOutDesc));
resOutDesc.resType = hipResourceTypeArray;
resOutDesc.res.array.array = hipOutArray;
hipSurfaceObject_t outSurfaceObject = 0;
hipCreateSurfaceObject(&outSurfaceObject, &resOutDesc);
float* dData = NULL;
2018-03-12 11:29:03 +05:30
hipMalloc((void**)&dData, size);
2018-03-04 22:49:23 +05:30
dim3 dimBlock(16, 16, 1);
dim3 dimGrid(width / dimBlock.x, height / dimBlock.y, 1);
2018-03-12 11:29:03 +05:30
hipLaunchKernelGGL(tex2DKernel, dim3(dimGrid), dim3(dimBlock), 0, 0, surfaceObject,
outSurfaceObject, width, height);
2018-03-04 22:49:23 +05:30
hipDeviceSynchronize();
2018-03-12 11:29:03 +05:30
float* hOutputData = (float*)malloc(size);
memset(hOutputData, 0, size);
hipMemcpyFromArray(hOutputData, hipOutArray, 0, 0, size, hipMemcpyDeviceToHost);
2018-03-04 22:49:23 +05:30
printf("dData: ");
for (int i = 0; i < 64; i++) {
printf("%f ", hOutputData[i]);
}
printf("\n");
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
2018-03-12 11:29:03 +05:30
if (hData[i * width + j] != hOutputData[i * width + j]) {
printf("Difference [ %d %d ]:%f ----%f\n", i, j, hData[i * width + j],
hOutputData[i * width + j]);
2018-06-02 10:58:03 +05:30
testResult = 0;
2018-03-04 22:49:23 +05:30
break;
}
}
}
hipDestroySurfaceObject(surfaceObject);
hipDestroySurfaceObject(outSurfaceObject);
hipFree(dData);
hipFreeArray(hipArray);
hipFreeArray(hipOutArray);
2018-06-02 10:58:03 +05:30
return testResult;
2018-03-04 22:49:23 +05:30
}