-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added memcpy_swap test
- Loading branch information
Shinpei Kato
committed
Dec 13, 2011
1 parent
ab641f3
commit d799917
Showing
7 changed files
with
227 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Makefile | ||
TARGET = user_test | ||
CC = gcc | ||
NVCC = nvcc -arch sm_20 -cubin | ||
LIBS = -lcuda -lgdev | ||
CFLAGS = -L /usr/local/gdev/lib64 -I /usr/local/gdev/include | ||
|
||
all: | ||
gcc -o $(TARGET) $(CFLAGS) $(LIBS) main.c | ||
|
||
clean: | ||
rm -f $(TARGET) *.cubin ./*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Makefile | ||
TARGET = user_test | ||
CC = gcc | ||
NVCC = nvcc -arch sm_20 -cubin | ||
LIBS = -lcuda | ||
CFLAGS = -I /usr/local/cuda/include | ||
|
||
all: | ||
gcc -o $(TARGET) $(CFLAGS) $(LIBS) main.c | ||
|
||
clean: | ||
rm -f $(TARGET) *.cubin ./*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
#include <cuda.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <sys/time.h> | ||
#include <sys/wait.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int i; | ||
pid_t pid; | ||
int status; | ||
unsigned int size = 0x10000000; /* 256MB */ | ||
size_t total_mem; | ||
CUresult res; | ||
CUdevice dev; | ||
CUcontext ctx; | ||
CUdeviceptr data_addr; | ||
unsigned int *in, *out; | ||
|
||
if (argc > 1) | ||
sscanf(argv[1], "%x", &size); | ||
|
||
res = cuInit(0); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuInit failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuDeviceGet(&dev, 0); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuDeviceGet failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuCtxCreate(&ctx, 0, dev); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuCtxCreate failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuDeviceTotalMem(&total_mem, dev); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuDeviceTotalMem failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
printf("Total Memory = 0x%x\n", total_mem); | ||
|
||
res = cuMemAlloc(&data_addr, size); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuMemAlloc failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
pid = fork(); | ||
|
||
if (pid == 0) { /* child */ | ||
res = cuInit(0); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuInit failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuDeviceGet(&dev, 0); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuDeviceGet failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuCtxCreate(&ctx, 0, dev); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuCtxCreate failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuMemAlloc(&data_addr, size); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuMemAlloc failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
in = (unsigned int *) malloc(size); | ||
out = (unsigned int *) malloc(size); | ||
for (i = 0; i < size / 4; i++) { | ||
in[i] = i+1; | ||
out[i] = 0; | ||
} | ||
|
||
res = cuMemcpyHtoD(data_addr, in, size); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuMemcpyHtoD failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuMemcpyDtoH(out, data_addr, size); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuMemcpyDtoH failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuMemFree(data_addr); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuMemFree failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuCtxDestroy(ctx); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuCtxDestroy failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
for (i = 0; i < size / 4; i++) { | ||
if (in[i] != out[i]) { | ||
printf("in[%d] = %u, out[%d] = %u\n", | ||
i, in[i], i, out[i]); | ||
break; | ||
} | ||
} | ||
|
||
free(in); | ||
free(out); | ||
|
||
printf("Child finished\n"); | ||
exit(-1); | ||
} | ||
else { /* parent */ | ||
waitpid(pid, &status, 0); | ||
|
||
in = (unsigned int *) malloc(size); | ||
out = (unsigned int *) malloc(size); | ||
for (i = 0; i < size / 4; i++) { | ||
in[i] = i+1; | ||
out[i] = 0; | ||
} | ||
|
||
res = cuMemcpyHtoD(data_addr, in, size); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuMemcpyHtoD failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuMemcpyDtoH(out, data_addr, size); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuMemcpyDtoH failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuMemFree(data_addr); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuMemFree failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
res = cuCtxDestroy(ctx); | ||
if (res != CUDA_SUCCESS) { | ||
printf("cuCtxDestroy failed: res = %u\n", (unsigned int)res); | ||
exit(-1); | ||
} | ||
|
||
for (i = 0; i < size / 4; i++) { | ||
if (in[i] != out[i]) { | ||
printf("in[%d] = %u, out[%d] = %u\n", | ||
i, in[i], i, out[i]); | ||
break; | ||
} | ||
} | ||
|
||
free(in); | ||
free(out); | ||
|
||
printf("Parent finished\n"); | ||
exit(0); | ||
} | ||
|
||
return 0; | ||
} |