2016-10-13 1 views
0

내 문제는Cudafy C# GPU 계산에서 값을 반환하는 방법은 무엇입니까?

이봐, 내가 (내 시스템을위한 벤치 마크로 사용으로) 0에서 100도 사이의 죄의 합을 찾으려면이 간단한 계산을 만들고있어, 계산은 아니다

코드

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; 


    } 

: 문제는 내 문제는 내가 Cudafy에 새로운 오전과 내가 제대로에서 통과하고 여기에 떨어져 인쇄 할 수 있도록 값을 반환 할 수 있겠군요임을 것은 내 코드입니다 내가하려고하는 가치 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); 
       } 
      } 
     } 
    } 

- 잭