Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi,

I want to use GPU by too many threads (CPU threads).When I load modules by once thread and call "EnableMultithreading()" method then call a GPU module's by another thread a "ErrorInvalidContext" exception accrued. (My system has only one GPU)

Here is my simply code:

C#
class Worker
  {
    public GPGPU gpu { get; set; }
    public int TID { get; set; }
    public void test(object stateObject)
    {
      int[] dev0 = gpu.Allocate<int>(512);
      int gridSize = 1;
      int blockSize = 512;
      gpu.Launch(gridSize, blockSize, "aaa", dev0);
      int[] host0 = new int[512];
      gpu.CopyFromDevice<int>(dev0, host0);
      gpu.FreeAll();

      Console.WriteLine(TID + " thread done.");

      Console.ReadKey();
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      GPGPU gpu = CudafyHost.GetDevice();
      CudafyModule module = CudafyModule.TryDeserialize();
      if (module == null || !module.TryVerifyChecksums())
      {
        module = CudafyTranslator.Cudafy();
        module.Serialize();
      }
      if (!gpu.IsModuleLoaded(module.Name))
      {
        gpu.LoadModule(module);
      }
      if (!gpu.IsMultithreadingEnabled)
      {
        gpu.EnableMultithreading();
      }
      

      List<Thread> threadArray = new List<Thread>();
      for (int i = 0; i < 10; i++)
      {
        Worker w = new Worker() { gpu = gpu, TID=i };
        Thread t = new Thread(w.test);
        t.Start();
        threadArray.Add(t);
      }
    }

    [Cudafy]
    public static void aaa(GThread thread, int[] a)
    {
      int tid = thread.threadIdx.x;
      a[tid] = 1;
    }


Please guide me how to use GPU by multithreads.

Thank you!
Zollie
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900