Beispiel #1
0
        private void ToIMem(object arg)
        {
            var x = arg.GetType();
            //Console.WriteLine(x);
            var isIMem = arg is IMem;

            if (isIMem)
            {
                curArgSize = ((IMem)arg).Size;
                curArgVal  = ((IMem)arg).Data;
            }
            else if (arg.GetType().ToString().EndsWith("[]"))
            {
                ArrayToMem(arg, arg.GetType().GetElementType());
            }
            else if (arg.GetType().ToString().Contains("System.Tuple"))
            {
                ToIMem(TupleConverter.TupleToMem(arg));
            }
            else
            {
                curArgSize = (System.IntPtr)System.Runtime.InteropServices.Marshal.SizeOf(arg);
                curArgVal  = arg;
            }
        }
Beispiel #2
0
 private void ArrayToMem(object data, System.Type t)
 {
     curArgSize = _intPtrSize;
     if (kernel.Provider.AutoconfiguredBuffers.ContainsKey(data))
     {
         curArgVal = kernel.Provider.AutoconfiguredBuffers[data];
     }
     else
     {
         ErrorCode error;
         var       operations   = Operations.ReadWrite;
         var       memory       = Memory.Device;
         var       _elementSize = 0;
         if (t.Name.Contains("Tuple"))
         {
             var arr   = ((Array)data);
             var types = arr.GetValue(0).GetType().GetProperties();
             if (types.Length == 2)
             {
                 var    a1       = types[0].PropertyType;
                 var    a2       = types[1].PropertyType;
                 Type   d1       = typeof(t2 <,>);
                 Type[] typeArgs = { a1, a2 };
                 Type   makeme   = d1.MakeGenericType(typeArgs);
                 var    data2    = Array.CreateInstance(makeme, arr.Length);
                 for (int i = 0; i < ((Array)data).Length; i++)
                 {
                     data2.SetValue(TupleConverter.TupleToMem(((Array)data).GetValue(i)), i);
                 }
                 data         = data2;
                 _elementSize = Marshal.SizeOf(data2.GetValue(0));
             }
             if (types.Length == 3)
             {
                 var    a1       = types[0].PropertyType;
                 var    a2       = types[1].PropertyType;
                 var    a3       = types[1].PropertyType;
                 Type   d1       = typeof(t3 <, ,>);
                 Type[] typeArgs = { a1, a2, a3 };
                 Type   makeme   = d1.MakeGenericType(typeArgs);
                 var    data2    = Array.CreateInstance(makeme, arr.Length);
                 for (int i = 0; i < ((Array)data).Length; i++)
                 {
                     data2.SetValue(TupleConverter.TupleToMem(((Array)data).GetValue(i)), i);
                 }
                 data         = data2;
                 _elementSize = Marshal.SizeOf(data2.GetValue(0));
             }
         }
         else
         {
             _elementSize = Marshal.SizeOf(t);
         }
         var mem = Cl.CreateBuffer(kernel.Provider.Context, (MemFlags)operations | (memory == Memory.Host ? MemFlags.UseHostPtr : (MemFlags)memory | MemFlags.CopyHostPtr),
                                   (IntPtr)(_elementSize * ((Array)data).Length), data, out error);
         curArgVal = mem;
         //mem.Pin();
         kernel.Provider.AutoconfiguredBuffers.Add(data, (Mem)mem);
         if (error != ErrorCode.Success)
         {
             throw new CLException(error);
         }
     }
 }