2016-10-13 8 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; 


    } 

ценность, которую я пытаюсь xtract из части CUDAfy - это сумма, а затем распечатать ее, а также распечатать время для бенчмаркинга. Если бы кто-нибудь мог опубликовать совет, он был бы очень признателен (также любые предложения по избавлению от любых бесполезных линий или неэффективных произведений тоже были бы хороши).

+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); 
       } 
      } 
     } 
    } 

-Джек

 Смежные вопросы

  • Нет связанных вопросов^_^