0

I am having memory corruption problems when I explicitly dispose IMem objects created by OpenCL.Net (v2.2.9). If I don't call IMem.Dispose() the video card memory is not released.

Unfortunately I found too few examples on the subject. Here a single call to env.Dispose() is performed.

My code is something like this:

IMem<int> idxsBuffer = context.CreateBuffer(idxs, MemFlags.ReadOnly);

for(int i=0; i<n; i++){
   IMem<float4>[] a = CreateA(); // c_.Length times context.CreateBuffer(a_[...], MemFlags.ReadOnly)
   for(int j=0; j<m; j++){
      b_ = CreateB(i, j);
      c_ = CreateC(i, j);
      for(int k=0; k<o; j++){
         IMem<float4> b = context.CreateBuffer(b_[k], MemFlags.ReadOnly);
         IMem<float> c = context.CreateBuffer<float>(c_[k].Length, MemFlags.WriteOnly);
         kernel.Run(cmdQueue, idxsBuffer, b, a[k], c, [...]);
         float[] u = new float[c_[k].Length];
         cmdQueue.ReadFromBuffer(c, u);
         b.Dispose(); // Cause problem
         c.Dispose(); // Cause problem
      }
   }
   foreach(IMem<float4> a_ in a){
      a_.Dispose(); // Cause problem
   }
}

To avoid memory corruption I have to comment out the lines that cause problems, but then I see a constant increase of the video card memory consumption.

Edit: I partially solved the problem avoiding the disposal of the objects during the iterations, reusing the same buffers. Still I can't understand what I was doing wrong.

stenio
  • 297
  • 1
  • 10
  • I've just tried to replicate the problem doing an example from scratch and it seems to work properly using the Dispose. Maybe there is an index out of bound in the kernel that corrupts the memory or a buffers overlap. – stenio Jul 23 '20 at 08:09
  • Updated the NVidia driver to the latest version but nothing changes. I have also tried to avoid the disposal and to reuse the buffers at every iteration (creating bigger buffers if necessary) but then the results differ from the version without disposal. – stenio Jul 25 '20 at 12:15
  • Looking at the OpenCL.Net source code I see that the CreateBuffer method I am calling uses the flag MemFlags.CopyHostPtr. I read from the API documentation: “ CL_MEM_COPY_HOST_PTR can be used with CL_MEM_ALLOC_HOST_PTR to initialize the contents of the cl_mem object allocated using host- accessible (e.g. PCIe) memory”. Maybe it is necessary to add this flag? – stenio Jul 26 '20 at 06:58

0 Answers0