最終更新: mikk_ni3_92 2008年05月01日(木) 17:34:33履歴
現在地 >> メニュー >> CUDA >> CUDA::基本::並列処理 >> CUDA::基本::並列処理2 >> CUDA::基本::並列処理3
#include <iostream>
#include <string>
#include <cuda_runtime.h>
using namespace std;
typedef struct
{
float Re;
float Im;
}COMPLEX;
/********[エラー処理]********/
namespace
{
void errorexit(string message)
{
cerr << message << endl;
exit(1);
}
}
/************************************************************************/
/* ここから「G」PUでの処理 */
/************************************************************************/
__global__ void ADD_REAL(COMPLEX *o_data,float value)
{
const int tid = threadIdx.x; //スレッドのId(x座標)取得
o_data[tid].Re += value ;
o_data[tid].Im += value ;
}
/*****************************************************************/
/* ここから 「C」PU での処理 */
/****************************************************************/
/**********************[CUDAの初期化]*********************/
void InitCUDA(void)
{
int count = 0;
int i = 0;
cudaGetDeviceCount(&count);
if(count == 0) {
errorexit("There is no device");
}
for( i = 0; i < count; i++) {
cudaDeviceProp prop;
if(cudaGetDeviceProperties(& prop, i) == cudaSuccess) {
if(prop.major >= 1) {
break;
}
}
}
if(i == count)
{
errorexit( "There is no device supporting CUDA 1.x.");
}
cudaSetDevice(i);
}
/******************** [メイン関数] *************************/
int main(int argc, char** argv)
{
InitCUDA();
COMPLEX *c = new COMPLEX[100];
for(int i = 0; i < 100;++i)
{
c[i].Re = static_cast<float>(i);
c[i].Im = static_cast<float>(i*i);
}
COMPLEX* gpu_c = NULL; //GPU
cudaMalloc( reinterpret_cast<void**>( &gpu_c), sizeof(COMPLEX) * 100);
if(gpu_c == NULL)
{
errorexit("can't cudaMalloc");
}
cudaMemcpy( gpu_c, c, sizeof(COMPLEX) * 100 , cudaMemcpyHostToDevice);
dim3 block(1,1,1);//ブロック1個
dim3 threads(100,1,1); //スレッド100個
ADD_REAL<<<block, threads,0>>>(gpu_c,10000);
cudaThreadSynchronize();
/* GPU => CPUへコピー */
cudaMemcpy( c, gpu_c, sizeof(COMPLEX) * 100, cudaMemcpyDeviceToHost);
cudaFree(gpu_c); gpu_c = NULL;
/* 出力 */
for( int i = 0; i < 100; i++)
{
cout << c[i].Re << " , " << c[i].Im << "\n";
}
delete [] c;c=NULL;
return 0;
}
#include <iostream>
#include <string>
#include <cuda_runtime.h>
using namespace std;
typedef struct
{
float Re;
float Im;
}COMPLEX;
/********[エラー処理]********/
namespace
{
void errorexit(string message)
{
cerr << message << endl;
exit(1);
}
}
/************************************************************************/
/* ここから「G」PUでの処理 */
/************************************************************************/
__global__ void ADD_REAL(COMPLEX *o_data,float value)
{
const int tid = threadIdx.x; //スレッドのId(x座標)取得
o_data[tid].Re += value ;
o_data[tid].Im += value ;
}
/*****************************************************************/
/* ここから 「C」PU での処理 */
/****************************************************************/
/**********************[CUDAの初期化]*********************/
void InitCUDA(void)
{
int count = 0;
int i = 0;
cudaGetDeviceCount(&count);
if(count == 0) {
errorexit("There is no device");
}
for( i = 0; i < count; i++) {
cudaDeviceProp prop;
if(cudaGetDeviceProperties(& prop, i) == cudaSuccess) {
if(prop.major >= 1) {
break;
}
}
}
if(i == count)
{
errorexit( "There is no device supporting CUDA 1.x.");
}
cudaSetDevice(i);
}
/******************** [メイン関数] *************************/
int main(int argc, char** argv)
{
InitCUDA();
COMPLEX *c = new COMPLEX[100];
for(int i = 0; i < 100;++i)
{
c[i].Re = static_cast<float>(i);
c[i].Im = static_cast<float>(i*i);
}
COMPLEX* gpu_c = NULL; //GPU
cudaMalloc( reinterpret_cast<void**>( &gpu_c), sizeof(COMPLEX) * 100);
if(gpu_c == NULL)
{
errorexit("can't cudaMalloc");
}
cudaMemcpy( gpu_c, c, sizeof(COMPLEX) * 100 , cudaMemcpyHostToDevice);
dim3 block(1,1,1);//ブロック1個
dim3 threads(100,1,1); //スレッド100個
ADD_REAL<<<block, threads,0>>>(gpu_c,10000);
cudaThreadSynchronize();
/* GPU => CPUへコピー */
cudaMemcpy( c, gpu_c, sizeof(COMPLEX) * 100, cudaMemcpyDeviceToHost);
cudaFree(gpu_c); gpu_c = NULL;
/* 出力 */
for( int i = 0; i < 100; i++)
{
cout << c[i].Re << " , " << c[i].Im << "\n";
}
delete [] c;c=NULL;
return 0;
}