Hi, everyone,
I write a program to implement executing a external cuda process.
But the process exited before running to cuda function call.
My cuda process code: process1.cu ( just no effect and loop for cuda kernel )
[codebox]
#include <cuda_runtime.h>
#include <cuda.h>
#include <sys/time.h>
#define LOOP_SIZE 39999999
#define LIST_NUM 100
#define GPU_LOOP 1
using namespace std;
#define TIMESTAMP(s); {\
timeval tv;\
char time_str[100];\
gettimeofday(&tv,NULL);\
strftime(time_str, 100, "%F %H:%M:%S", localtime(&tv.tv_sec));\
cout << time_str << ":" << tv.tv_usec << " KProcess-1: " << s << endl;\
}
global void Kernel_dataCount(long* d_list, int num, clock_t* time)
{
clock_t start = clock();
int index = blockIdx.x * blockDim.x + threadIdx.x;
for(int j = 0; j < GPU_LOOP; j ++){
for(long i = 0; i < LOOP_SIZE; i ++){
d_list[index] = num;
}
}
*time = clock() - start;
}
void dataCount(int num)
{
// GPU clock
clock_t* d_time;
clock_t endTime;
long *d_list;
int size = LIST_NUM * sizeof(long);
long *list = new long[LIST_NUM];
for(int i = 0; i < LIST_NUM; i++){
list[i] = -1;
}
TIMESTAMP("Start!");
// allocate resource
cudaMalloc((void**) &d_list, size);
cudaMalloc((void**) &d_time, sizeof(clock_t));
cudaMemcpy(d_list, list, size, cudaMemcpyHostToDevice);
// set Grid/Block/Thread
if(LIST_NUM > 256){
cout << "Error num" << endl;
exit(1);
}
dim3 dimBlock(LIST_NUM);
dim3 dimGrid(1);
// run Kernel function
TIMESTAMP("Call Kernel Function!");
Kernel_dataCount<<<dimGrid, dimBlock>>>(d_list, num, d_time);
TIMESTAMP("Call Kernel Function done!");
// block until the device has completed
cudaThreadSynchronize();
TIMESTAMP("Synchronize done!");
// Read result from device memory
cudaMemcpy(list, d_list, size, cudaMemcpyDeviceToHost);
cudaMemcpy(&endTime, d_time, sizeof(clock_t), cudaMemcpyDeviceToHost);
TIMESTAMP("cudaMemcpy done!");
for(int i = 0; i < LIST_NUM; i++){
if (list[i] != num){
cout << "!!Error!! list[" << i << "]= " << list[i] << endl;
}
}
// Free device memory
cudaFree(d_list);
TIMESTAMP("cudaFree done!");
}
int main(int argc, char *argv)
{
int num=atoi(argv[1]);
TIMESTAMP("Program Start!");
dataCount(num);
TIMESTAMP("Program Exit!");
return 0;
}
[/codebox]
My main process code: main.cpp
[codebox]
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
} else if (pid == 0){ // child-1
TIMESTAMP("Child process-1 fork!");
char* argv[] = {(char *)"process1", (char *)"10", NULL};
char* envp[] = {(char *)"LD_LIBRARY_PATH=/usr/local/cuda/lib", NULL};
int rt = execve("./process1", argv, envp);
if (rt == -1)
cout << "!!ERROR!!" << endl;
exit(0);
}
[/codebox]
code.tar.gz (209 KB)
The process1 just print out “KProcess-1: Program Start!†and “KProcess-1: Start!â€
I don’t know why it doesn’t work.
And I want to test the correctness about fork many cuda process at the same time.
But now I even one cuda process is not work.
I have no idea to solve the problem.
My SDK version 2.3, g++ version 4.3
Thanks for your helps.