cudasharp--allows your C # code on the GPU

Source: Internet
Author: User

The CUDASHARP project is designed to allow you to run a C # program on a GPU that supports Cuda.

Sample code:

using system;
using cudasharp;

using managedcuda;
namespace cudasharptest {    static class program     {         static void main ()          {            var ptx =
 CudaSharp.CudaSharp.Translate<int[]> (kernel);
            test (PTX);
            console.readkey (TRUE);         }         static  Void store (int[] arr, int value)         {  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ARR[GPU.THREADX ()  + Gpu.BlockX () &NBSP;*&NBSP;GPU.THREADDIMX ()]&NBSP;=&NBSp;value;         }         //  Resharper disable once inconsistentnaming         static  void kernel (Int[] arr)         {    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VAR&NBSP;TID&NBSP;=&NBSP;GPU.THREADX ()  + 
Gpu.blockx ()  * gpu.threaddimx ();
            var val = arr[tid];             if  (val != 0)                  store (Arr, val
&NBSP;+&NBSP;3);         }         static  Void test (Byte[] ptxfile)         {            const int
 size = 16;             var context = new 
Cudacontext ();             var kernel = context.
Loadkernelptx (ptxfile,  "kernel");             var memory = context.
Allocatememory (4 * size);             var gpumemory = new
 CudaDeviceVariable<int> (memory);             var cpumemory = new
 int[size];             for  (var i = 0;  i < size; i++)                 cpumemory[i] = i - 2;
            gpumemory.copytodevice (cpuMemory);             kernel.
blockdimensions = 4;             kernel.
griddimensions = 4;             kernel.
Run (memory);
            gpumemory.copytohost (cpuMemory);             for  (var i = 0;  i < size; i++)             
    console.writeline ("{0} = {1}",  i, cpumemory[i]);         }     }
 

}

Turn from: http://www.oschina.net/p/cudasharp?utm_source=tuicool&utm_medium=referral

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.