- Device : Tesla C2050
- OS : Windows 7 Enterprise
- IDE : VS 2010
- CUDA : 5.0 (newest)
First time to ask question here. I met some problems in my CUDA program.
I have millions tetrahedrons with one point at (0,0,0), so I can use the formula:
to get the volume of the tetrahedrons.
So , here is the code:
struct Triangle
{
double x1;
double y1;
double z1;
double x2;
double y2;
double z2;
double x3;
double y3;
double z3;
};
And the CUDA code:
__global__ void getResult(double *d_volume ,Triangle *d_triangles, Origin *d_point)
{
extern __shared__ Triangle s_data[];
int tid = threadIdx.x;
int i = blockDim.x * blockIdx.x + threadIdx.x;
s_data[tid] = d_triangles[i];
__syncthreads();
d_volume[i] =s_data[tid].x1 * s_data[tid].y2 * s_data[tid].z3 + \
s_data[tid].y1 * s_data[tid].z2 * s_data[tid].x3 + \
s_data[tid].x2 * s_data[tid].y3 * s_data[tid].z1 - \
s_data[tid].x3 * s_data[tid].y2 * s_data[tid].z1 - \
s_data[tid].x2 * s_data[tid].y1 * s_data[tid].z3 - \
s_data[tid].y3 * s_data[tid].z2 * s_data[tid].x1;
}
I got millions of tetrahedrons from other function as an array.
// Host
Triangle *h_triangles = triangles;
double *h_volume;
// Device
Triangle *d_triangles;
double *d_volume;
// define grid and block size
int numThreadsPerBlock = numTriangles;
int numBlocks = numTrianges / 512;
// Shard memory size
int sharedMemSize = numThreadsPerBlock * sizeof(Triangle);
// allocate host and device memory
size_t memSize_triangles = numBlocks * numThreadsPerBlock * sizeof(Triangle);
size_t memSize_volume = numBlocks * numThreadsPerBlock * sizeof(double);
cudaMalloc( (void **) &d_triangles, memSize_triangles );
cudaMalloc( (void **) &d_volume, memSize_volume );
// Copy host array to device array
cudaMemcpy( d_triangles, h_triangles, memSize_triangles, cudaMemcpyHostToDevice );
cudaMemcpy( d_point, h_point, memSize_point, cudaMemcpyHostToDevice );
// launch kernel
dim3 dimGrid(numBlocks);
dim3 dimBlock(numThreadsPerBlock);
getResult<<< dimGrid, dimBlock, sharedMemSize >>>( d_volume, d_triangles);
// block until the device has completed
cudaThreadSynchronize();
// device to host copy
cudaMemcpy( h_volume, d_volume, memSize_volume, cudaMemcpyDeviceToHost );
// free device memory
cudaFree(d_triangles);
cudaFree(d_volume);
// free host memory
free(h_triangles);
free(h_volume);
Up to now, everything works OK. But I cost more time than I thought to get the volume. My device is Tesla C2050(515Gflops), 20 times faster than my CPU(single-core, 20.25Gflops). But only speed up about 10 times (not including the time copying memory between device and host.)
I'd like to know how can I make it about 20 times faster than the CPU code (for loop to get the volume.).
Thanks!
PS: Maybe cudaMallocPitch() will help me, but the triangles are not matrix, I can't use cudaMemcpy2D() to copy memory instead of cudaMemcpy(). Anyone who can help me about this question?