2016-10-13 3 views
0

私の問題はCudafy C#GPUの計算から値を返すには?

ねえ、私は(私は私のシステムのためのベンチマークとしてそれを使用するように)0と100度の間の罪の合計を見つけるために、この単純な計算を作ってるんだ、計算ではありません私の問題は、私はCudafyに新しいですし、私は適切に渡すと、それはここでオフに印刷することができるように値を返す方法についてはわからないだということである問題が私のコードは次のとおりです。

コード

public const int N = 33 * 1024; 
    public const int threadsPerBlock = 256; 
    public const int blocksPerGrid = 32;           

    public static void Main() 
    { 
     Stopwatch watch = new Stopwatch();           
     watch.Start();                
     string Text = ""; 
     int iterations = 1000000; 
     CudafyModule km = CudafyTranslator.Cudafy(); 
     GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId); 
     gpu.LoadModule(km); 
     double[] dev_Value = gpu.Allocate<double>(); 
     gpu.Launch(blocksPerGrid, threadsPerBlock).SumOfSines(iterations,dev_Value);              

     double Value; 
     gpu.CopyFromDevice(dev_Value, out Value); 
     watch.Stop();                         
     Text = watch.Elapsed.TotalSeconds.ToString();                 
     Console.WriteLine("The process took a total of: " + Text + " Seconds"); 
     Console.WriteLine(Value); 
     Console.Read(); 
     gpu.FreeAll(); 
    } 
    [Cudafy] 
    public static void SumOfSines(GThread thread,int iterations,double [] Value) 
    { 
     double total = new double(); 
     double degAsRad = Math.PI/180.0; 
     for (int i = 0; i < iterations; i++) 
     { 
      total = 0.0; 
      for (int z = 1; z < 101; z++) 
      { 
       double angle = (double)z * degAsRad; 
       total += Math.Sin(angle); 
      } 

     } 
     Value[0] = total; 


    } 

私がeにしようとしている価値CUDAfy部分からのxtractは合計であり、ベンチマークの時間を印刷するだけでなく、それを印刷します。もし誰かがアドバイスを投稿できれば、非常に感謝しています(無駄な行や不十分な部分を取り除くための提案も良いでしょう)。

+0

をそして、それは 'dev_Value [0]'ではないでしょうか? – Andrew

答えて

1

は、私は答えを見つけることが重要ではありませんが、私はそれをここに投稿します:

public const int N = 33 * 1024; 
    public const int threadsPerBlock = 256; 
    public const int blocksPerGrid = 32; 

    public static void Main() 
    { 
     Stopwatch watch = new Stopwatch(); 
     watch.Start(); 
     CudafyModule km = CudafyTranslator.Cudafy(); 

     GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId); 
     gpu.LoadModule(km); 

     string Text = ""; 
     int iterations = 1000000; 
     double Value; 
     double[] dev_Value = gpu.Allocate<double>(iterations * sizeof(double)); 
     gpu.Launch(blocksPerGrid, threadsPerBlock).SumOfSines(iterations, dev_Value); 
     gpu.CopyFromDevice(dev_Value, out Value); 
     watch.Stop(); 
     Text = watch.Elapsed.TotalSeconds.ToString(); 
     Console.WriteLine("The process took a total of: " + Text + " Seconds"); 
     Console.WriteLine(Value); 
     Console.Read(); 
     gpu.FreeAll(); 
    } 

    [Cudafy] 
    public static void SumOfSines(GThread thread, int _iterations, double[] Value) 
    { 
     int threadID = thread.threadIdx.x + thread.blockIdx.x * thread.blockDim.x; 
     int numThreads = thread.blockDim.x * thread.gridDim.x; 
     if (threadID < _iterations){ 
      for (int i = threadID; i < _iterations; i += numThreads) 
      { 
       double _degAsRad = Math.PI/180; 
       Value[i] = 0.0; 
       for (int a = 0; a < 100; a++) 
       { 
        double angle = (double)a * _degAsRad; 
        Value[i] += Math.Sin(angle); 
       } 
      } 
     } 
    } 

-Jack

関連する問題