c++ AMP library with c#: How to keep data in GPU memory? -
let's have program generates big randomly filled integer array , allows me check amount of items divisible user-inputted number, using gpu purpose.
c# code
[dllimport("amp.dll", charset = charset.unicode, callingconvention = callingconvention.cdecl)] public static extern int runtest(int* cpuinput, int length, int num); static void main(string[] args) { random rnd = new random(); int[] arr = new int[10000000]; (int = 0; < arr.length; i++) arr[i] = rnd.next(1, int.maxvalue); fixed (int* arrptr = &arr[0]) { while (true) { int num = int.parse(console.readline()); console.writeline($"there {runtest(arrptr, arr.length, num)} numbers in array divisible {num}"); } } }
c++ code
extern "c" { __declspec(dllexport) int runtest(int* input, int length, int num); } int runtest(int* cpuinput, int length, int num) { int cpuresult[1]; cpuresult[0] = 0; array_view<int, 1> gpuinput(length, cpuinput); array_view<int, 1> gpuresult(1, cpuresult); parallel_for_each(gpuinput.get_extent(), [=](index<1> idx) restrict(amp) { if (gpuinput[idx[0]] % num == 0) atomic_fetch_inc(&gpuresult[0]); }); gpuinput.discard_data(); gpuresult.synchronize(); return cpuresult[0]; }
obviously, copying array each time run test bad idea. in fact bottleneck in case. how can store array in gpu memory among several library calls?
i've done long time ago. want create wrapper in c++ cli , interop c# code has can maintain reference , keep memory on gpu allocated.
the following should started
Comments
Post a Comment