Beispiel #1
0
        public void CreateImageTests()
        {
            ErrorCode error;

            if (Cl.GetDeviceInfo(_device, DeviceInfo.ImageSupport, out error).CastTo <Bool>() == Bool.False)
            {
                Console.WriteLine("No image support");
                return;
            }

            {
                var  image2DData = new float[200 * 200 * sizeof(float)];
                IMem image2D     = Cl.CreateImage2D(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, new ImageFormat(ChannelOrder.RGBA, ChannelType.Float),
                                                    (IntPtr)200, (IntPtr)200, (IntPtr)0, image2DData, out error);
                Assert.AreEqual(error, ErrorCode.Success);

                Assert.AreEqual(Cl.GetImageInfo(image2D, ImageInfo.Width, out error).CastTo <uint>(), 200);
                Assert.AreEqual(Cl.GetImageInfo(image2D, ImageInfo.Height, out error).CastTo <uint>(), 200);

                image2D.Dispose();
            }

            {
                var  image3DData = new float[200 * 200 * 200 * sizeof(float)];
                IMem image3D     = Cl.CreateImage3D(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, new ImageFormat(ChannelOrder.RGBA, ChannelType.Float),
                                                    (IntPtr)200, (IntPtr)200, (IntPtr)200, IntPtr.Zero, IntPtr.Zero, image3DData, out error);
                Assert.AreEqual(error, ErrorCode.Success);

                Assert.AreEqual(Cl.GetImageInfo(image3D, ImageInfo.Width, out error).CastTo <uint>(), 200);
                Assert.AreEqual(Cl.GetImageInfo(image3D, ImageInfo.Height, out error).CastTo <uint>(), 200);
                Assert.AreEqual(Cl.GetImageInfo(image3D, ImageInfo.Depth, out error).CastTo <uint>(), 200);

                image3D.Dispose();
            }
        }
Beispiel #2
0
        public void MemBufferTests()
        {
            const int bufferSize = 100;

            ErrorCode error;
            Random    random = new Random();

            float[] values = (from value in Enumerable.Range(0, bufferSize) select(float) random.NextDouble()).ToArray();
            IMem    buffer = Cl.CreateBuffer(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, (IntPtr)(sizeof(float) * bufferSize), values, out error);

            Assert.AreEqual(error, ErrorCode.Success);

            Assert.AreEqual(Cl.GetMemObjectInfo(buffer, MemInfo.Type, out error).CastTo <MemObjectType>(), MemObjectType.Buffer);
            Assert.AreEqual(Cl.GetMemObjectInfo(buffer, MemInfo.Size, out error).CastTo <uint>(), values.Length * sizeof(float));

            // TODO: Verify values
            //int index = 0;
            //foreach (float value in Cl.GetMemObjectInfo(buffer, Cl.MemInfo.HostPtr, out error).CastToEnumerable<float>(Enumerable.Range(0, 100)))
            //{
            //    Assert.AreEqual(values[index], value);
            //    index++;
            //}

            buffer.Dispose();
        }
 // IMem
 public static KernelArgChain SetKernelArg(this Kernel kernel, IMem buffer)
 {
     Cl.SetKernelArg(kernel, 0, TypeSize <IntPtr> .Size, buffer).Check();
     return(new KernelArgChain {
         Kernel = kernel, Count = 0
     });
 }
        // images cannot be read_write... so let's continue using plain buffers
        // should implement this in a way that allows imgfAccu to be loaded only once
        // should test for image size consistency
        public void Accumulate(FloatMap imgfAccu, FloatMap imgfSrc, float k)
        {
            var kernel = _kernels["accumulate"];

            // Creation of on-device memory objects
            IMem <float> accuMapBuffer = Cl.CreateBuffer <float>(_context, MemFlags.ReadWrite, imgfAccu.Size, out err); // why MemFlags.CopyHostPtr doesn't work here (and forces me to manual copy) ???

            assert(err, "accu buf creation");

            IMem <float> srcMapBuffer = Cl.CreateBuffer <float>(_context, MemFlags.WriteOnly, imgfSrc.Size, out err);

            assert(err, "src buf creation");

            // Set memory objects as parameters to kernel
            err = Cl.SetKernelArg(kernel, 0, intPtrSize, accuMapBuffer);
            assert(err, "accu map setKernelArg");

            err = Cl.SetKernelArg(kernel, 1, intPtrSize, srcMapBuffer);
            assert(err, "src map setKernelArg");

            err = Cl.SetKernelArg(kernel, 2, intSize, imgfAccu.Stride);
            assert(err, "in stride setKernelArg");

            err = Cl.SetKernelArg(kernel, 3, intSize, imgfSrc.Stride);
            assert(err, "out stride setKernelArg");

            err = Cl.SetKernelArg(kernel, 4, floatSize, k);
            assert(err, "out stride setKernelArg");

            // write actual data into memory object
            Event clevent;

            err = Cl.EnqueueWriteBuffer <float>(_commandsQueue, accuMapBuffer, Bool.True, 0, imgfAccu.Size, imgfAccu._buf, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "write accu buffer");

            err = Cl.EnqueueWriteBuffer <float>(_commandsQueue, srcMapBuffer, Bool.True, 0, imgfSrc.Size, imgfSrc._buf, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "write src buffer");

            // execute
            err = Cl.EnqueueNDRangeKernel(_commandsQueue, kernel, 2,
                                          new[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 },                           // offset
                                          new[] { new IntPtr(imgfAccu.W), new IntPtr(imgfAccu.H), (IntPtr)1 }, // range
                                          null, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "Cl.EnqueueNDRangeKernel");

            // sync
            Cl.Finish(_commandsQueue);

            // read from output memory object into actual buffer
            err = Cl.EnqueueReadBuffer <float>(_commandsQueue, accuMapBuffer, Bool.True, imgfAccu._buf, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "read output buffer");

            Cl.ReleaseMemObject(srcMapBuffer);
            Cl.ReleaseMemObject(accuMapBuffer); // maybe i could return this without disposing; would affect non-OpenCl implementation
        }
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            var user = IMem.GetUser8Name(username);

            user.Frozen = true;
            IMem.UpdateUser(user);
            return(true);
        }
Beispiel #6
0
        public static long CrackHigh(int[] sequence, long low)
        {
            if (!crackHighProgram.HasValue || !crackHighKernel.HasValue || !initialized || sequence.Length != 16)
            {
                return(-1);
            }

            ErrorCode error;

            IMem <int> sequence_dev = Cl.CreateBuffer <int>(context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, sequence, out error);

            ErrorCheck(error, "CrackHigh(): Cl.CreateBuffer");

            long[]      seeds    = new long[1];
            IMem <long> seed_dev = Cl.CreateBuffer <long>(context, MemFlags.CopyHostPtr | MemFlags.WriteOnly, seeds, out error);

            ErrorCheck(error, "InitializeParameters(): Cl.CreateBuffer");

            error = Cl.SetKernelArg(crackHighKernel.Value, 0, sequence_dev);
            ErrorCheck(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(crackHighKernel.Value, 1, seed_dev);
            ErrorCheck(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(crackHighKernel.Value, 2, (IntPtr)sizeof(long), low);
            ErrorCheck(error, "Cl.SetKernelArg");

            CommandQueue cmdQueue = Cl.CreateCommandQueue(context, device, 0, out error);

            ErrorCheck(error, "Cl.CreateCommandQueue");

            int maxGroupWorkSize = Cl.GetKernelWorkGroupInfo(crackHighKernel.Value, device, KernelWorkGroupInfo.WorkGroupSize, out error).CastTo <int>();

            ErrorCheck(error, "Cl.GetKernelWorkGroupInfo");

            Event e;
            int   threads = maxGroupWorkSize * 12;

            IntPtr[] workSize = new IntPtr[] { (IntPtr)threads };
            error = Cl.EnqueueNDRangeKernel(cmdQueue, crackHighKernel.Value, 1, null, workSize, null, 0, null, out e);
            ErrorCheck(error, "Cl.EnqueueNDRangeKernel");

            error = Cl.Finish(cmdQueue);
            ErrorCheck(error, "Cl.Finish");

            long[] seed_host = new long[1];
            error = Cl.EnqueueReadBuffer(cmdQueue, seed_dev, Bool.True, (IntPtr)0, (IntPtr)(sizeof(long) * 1), seed_host, 0, null, out e);
            ErrorCheck(error, "CL.EnqueueReadBuffer");

            //Dispose your shit
            error = Cl.ReleaseCommandQueue(cmdQueue);
            ErrorCheck(error, "CL.ReleaseCommandQueue");

            error = Cl.ReleaseMemObject(sequence_dev);
            ErrorCheck(error, "CL.ReleaseMemObject");

            error = Cl.ReleaseMemObject(seed_dev);
            ErrorCheck(error, "CL.ReleaseMemObject");
            return(seed_host[0]);
        }
        public void singlePass(Kernel kernel, FloatMap inMap, FloatMap outMap)
        {
            var clInImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.Luminance, ChannelType.Float);

            IMem inputMapBuffer = Cl.CreateImage2D(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, clInImageFormat,
                                                   (IntPtr)inMap.W, (IntPtr)inMap.H, new IntPtr(inMap.Stride * sizeof(float)),
                                                   inMap._buf, out err);

            assert(err, "input img creation");


            IMem outputMapBuffer = Cl.CreateImage2D(_context, MemFlags.WriteOnly, clInImageFormat,
                                                    (IntPtr)outMap.W, (IntPtr)outMap.H, new IntPtr(outMap.Stride * sizeof(float)),
                                                    outMap._buf, out err);

            assert(err, "output img creation");


            // Set memory objects as parameters to kernel
            err = Cl.SetKernelArg(kernel, 0, intPtrSize, inputMapBuffer);
            assert(err, "input map setKernelArg");

            err = Cl.SetKernelArg(kernel, 1, intPtrSize, outputMapBuffer);
            assert(err, "output map setKernelArg");


            // write actual data into memory object
            IntPtr[] originPtr        = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };               //x, y, z
            IntPtr[] inRegionPtr      = new IntPtr[] { (IntPtr)inMap.W, (IntPtr)inMap.H, (IntPtr)1 };   //x, y, z
            IntPtr[] outRegionPtr     = new IntPtr[] { (IntPtr)outMap.W, (IntPtr)outMap.H, (IntPtr)1 }; //x, y, z
            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)outMap.W, (IntPtr)outMap.H, (IntPtr)1 };
            Event    clevent;

            //err = Cl.EnqueueWriteImage(_commandsQueue, inputMapBuffer, Bool.True, originPtr, inRegionPtr, (IntPtr)0, (IntPtr)0, inMap._buf, 0, null, out clevent);
            //clevent.Dispose();
            //assert(err, "write input img");

            // execute
            err = Cl.EnqueueNDRangeKernel(_commandsQueue, kernel, 2,
                                          originPtr,
                                          workGroupSizePtr,
                                          null, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "Cl.EnqueueNDRangeKernel");

            // sync
            Cl.Finish(_commandsQueue);

            // read from output memory object into actual buffer
            err = Cl.EnqueueReadImage(_commandsQueue, outputMapBuffer, Bool.True, originPtr, outRegionPtr, new IntPtr(outMap.Stride * sizeof(float)), (IntPtr)0, outMap._buf, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "read output buffer");

            Cl.ReleaseMemObject(inputMapBuffer);
            Cl.ReleaseMemObject(outputMapBuffer);
        }
Beispiel #8
0
 public KernelImage(int index, MemFlags flags, byte[] bytes, int width, int height, ImageFormat pixelFormat)
     : this(index, flags, width, height, pixelFormat)
 {
     Source    = bytes;
     GpuBuffer = Cl.CreateImage2D(EasyClCompiler.Context, (OpenCL.Net.MemFlags)Flags, ImageFormat, (IntPtr)Width, (IntPtr)Height, (IntPtr)0, Source, out ErrorCode error);
     if (error != ErrorCode.Success)
     {
         throw new GPUException("SetImage2D", error.ToString());
     }
 }
Beispiel #9
0
 public static ErrorCode EnqueueWriteBuffer <T>(CommandQueue commandQueue,
                                                IMem <T> buffer,
                                                Bool blockingWrite,
                                                T[] data,
                                                int numEventsInWaitList,
                                                Event[] eventWaitList,
                                                out Event e) where T : struct
 {
     return(EnqueueWriteBuffer(commandQueue, buffer, blockingWrite, 0, data.Length, data, numEventsInWaitList, eventWaitList, out e));
 }
        public static void WriteToBuffer <T>(this CommandQueue commandQueue, IMem buffer, T[] data, int offset = 0, long length = -1, params Event[] waitFor)
            where T : struct
        {
            Event e;
            var   elemSize = TypeSize <T> .SizeInt;

            Cl.EnqueueWriteBuffer(commandQueue, buffer, Bool.True, (IntPtr)(offset * elemSize), (IntPtr)((length == -1 ? data.Length : length) * elemSize), data,
                                  (uint)waitFor.Length, waitFor.Length == 0 ? null : waitFor, out e)
            .Check();
            e.Dispose();
        }
 public override bool ValidateUser(string username, string password)
 {
     MODEL.User user = IMem.GetUser8Name(username);
     if (user == null)
     {
         return(false);
     }
     else
     {
         return(user.Password == password);
     }
 }
        public ClBuffer(Context context, int size, out ErrorCode error)
        {
            ready = false;

            this.Size = size;
            buffer    = new T[size];
            clMem     = Cl.CreateBuffer <T>(context, MemFlags.UseHostPtr, buffer, out error);

            if (error == ErrorCode.Success)
            {
                ready = true;
            }
        }
Beispiel #13
0
        public static ErrorCode EnqueueWriteBuffer <T>(CommandQueue commandQueue,
                                                       IMem <T> buffer,
                                                       Bool blockingWrite,
                                                       int offset,
                                                       int length,
                                                       T[] data,
                                                       int numEventsInWaitList,
                                                       Event[] eventWaitList,
                                                       out Event e) where T : struct
        {
            var elementSize = (int)TypeSize <T> .Size;

            return(EnqueueWriteBuffer(commandQueue, buffer, blockingWrite, (IntPtr)(offset * elementSize), (IntPtr)(length * elementSize), data, (uint)numEventsInWaitList, eventWaitList, out e));
        }
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            var user = IMem.GetUser8Id((Guid)providerUserKey);

            if (user == null)
            {
                return(null);
            }
            MembershipUser mu = new MembershipUser("memProvider", user.Name,
                                                   user.ID, user.Email, string.Empty, string.Empty, true, true, user.CreateTime, DateTime.Now,
                                                   DateTime.Now, DateTime.Now, DateTime.Now);

            return(mu);
        }
        public GpuSimiulation(IStockData stockData, int holding = 10, float ann = 5.0990f)
        {
            _stockData     = stockData;
            _holding       = holding;
            _ann           = ann;
            _programSource = File.ReadAllText("Kernels.cl");

            _env = "*AMD*".CreateCLEnvironment(DeviceType.Gpu, CommandQueueProperties.ProfilingEnable);

            _h_output           = new float[(stockData.QuotesPerStock - holding)];
            _d_stocksAndPrices  = Cl.CreateBuffer <float>(_env.Context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, _stockData.RawData, out ErrorCode err);
            _d_portfolioStockMv = Cl.CreateBuffer <float>(_env.Context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, _stockData.MarketValues, out err);
            _d_output           = Cl.CreateBuffer <float>(_env.Context, MemFlags.ReadWrite, stockData.StocksCount * (stockData.QuotesPerStock - holding), out err);
        }
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            MODEL.User user = IMem.GetUser8Name(username);
            if (user == null)
            {
                return(null);
            }
            MembershipUser mu = new MembershipUser("memProvider",
                                                   username, user.ID, user.Email, string.Empty, string.Empty,
                                                   true, true, user.CreateTime,
                                                   DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);

            return(mu);
        }
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            var user = IMem.GetUser8Name(username);

            if (user.Password == oldPassword)
            {
                user.Password = newPassword;
                IMem.UpdateUser(user);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #18
0
 public void EnqueueWriteBuffer <T>(IMem <T> buffer, int offset, int length, T[] data)
     where T : struct
 {
     Cl.EnqueueWriteBuffer(
         CommandQueue,
         buffer,
         Bool.True,
         offset,
         length,
         data,
         0,    //_awaitEvent == null ? 0 : 1,
         null, //_awaitEvent == null ? new Event[0] : new Event[]{ _awaitEvent.Value },
         out var awaitEvent
         );
     //_awaitEvent = awaitEvent;
 }
Beispiel #19
0
        public static InfoBuffer GetMemObjectInfo(IMem mem, MemInfo paramName, out ErrorCode error)
        {
            if (paramName == MemInfo.HostPtr) // Handle special case
            {
                IntPtr size   = GetInfo(Cl.GetMemObjectInfo, mem, MemInfo.Size, out error).CastTo <IntPtr>();
                var    buffer = new InfoBuffer(size);
                error = GetMemObjectInfo(mem, paramName, size, buffer, out size);
                if (error != ErrorCode.Success)
                {
                    return(InfoBuffer.Empty);
                }

                return(buffer);
            }

            return(GetInfo(GetMemObjectInfo, mem, paramName, out error));
        }
Beispiel #20
0
        public KernelImage(int index, MemFlags flags, Bitmap bmp) : this(index, flags, bmp.Width, bmp.Height, bmp.PixelFormat)
        {
            Rectangle bmpRect = new Rectangle(Point.Empty, bmp.Size);

            SystemGDI.BitmapData bmpData = bmp.LockBits(bmpRect, SystemGDI.ImageLockMode.ReadWrite, bmp.PixelFormat);
            IntPtr bmpPtr    = bmpData.Scan0;
            int    byteCount = bmpData.Stride * Height;

            Source = new byte[byteCount];
            Marshal.Copy(bmpPtr, Source, 0, byteCount);
            GpuBuffer = Cl.CreateImage2D(EasyClCompiler.Context, (OpenCL.Net.MemFlags)flags, ImageFormat, (IntPtr)Width, (IntPtr)Height, (IntPtr)0, Source, out ErrorCode error);
            if (error != ErrorCode.Success)
            {
                throw new GPUException("SetImage2D", error.ToString());
            }
            bmp.UnlockBits(bmpData);
        }
Beispiel #21
0
        public static void ImportFile(string filename, IMem memobject)
        {
            var parts = Path.GetFileNameWithoutExtension(filename).Split('-').Select(x => Convert.ToInt32(x)).ToArray();
            Uri fname = new Uri(filename, UriKind.RelativeOrAbsolute);



            BmpBitmapDecoder bd = new BmpBitmapDecoder(fname, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);

            int width  = parts[1];
            int height = parts[2];
            int size   = width * height;
            var data   = new byte[size];

            bd.Frames.Single().CopyPixels(data, width, 0);

            memobject.writepixels(data, parts[0], size);
        }
Beispiel #22
0
        public void Dispose()
        {
            UnlockBits();

            original = null;

            if (clMem != null)
            {
                Cl.ReleaseMemObject(clMem);
                clMem = null;
            }

            if (bitmapData != null)
            {
                bitmapData = null;
            }

            ready = false;
        }
Beispiel #23
0
        /// <summary>
        /// Writes an image to GPU memeory
        /// </summary>
        /// <param name="image">the image to write</param>
        /// <param name="memFlags">the memory flags</param>
        public void WriteImage(Bitmap image, MemFlags memFlags)
        {
            ErrorCode error;

            _image = Cl.CreateImage2D(_handle.Context, memFlags, new OpenCL.Net.ImageFormat(ChannelOrder.RGBA, ChannelType.Unorm_Int8),
                                      new IntPtr(image.Width), new IntPtr(image.Height), new IntPtr(0), new IntPtr(0), out error);
            CLException.CheckException(error);

            byte[]   data        = BmpToBytes(image);
            IntPtr[] originArray = new IntPtr[] { new IntPtr(0), new IntPtr(0), new IntPtr(0) };
            IntPtr[] sizeArray   = new IntPtr[] { new IntPtr(image.Width), new IntPtr(image.Height), new IntPtr(1) };

            Event e;

            error = Cl.EnqueueWriteImage(_handle.Queue, _image, Bool.True,
                                         originArray, sizeArray, new IntPtr(0), new IntPtr(0),
                                         data, 0, null, out e);
            CLException.CheckException(error);
        }
Beispiel #24
0
        public T[] ReadBuffer <T>(IMem <T> buffer, int offset, int length)
            where T : struct
        {
            var data = new T[length];

            Cl.EnqueueReadBuffer(
                CommandQueue,
                buffer,
                Bool.True,
                offset,
                length,
                data,
                0,    //_awaitEvent == null ? 0 : 1,
                null, //_awaitEvent == null ? new Event[0] : new Event[]{ _awaitEvent.Value },
                out var awaitEvent
                );
            //s_awaitEvent = awaitEvent;
            return(data);
        }
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            var exsit = GetUser(username, true);

            var user = new MODEL.User()
            {
                Name          = username,
                Password      = password,
                Opentype      = MODEL.OpenType.localuser,
                CreateTime    = DateTime.Now,
                LastLoginTime = DateTime.Now
            };

            IMem.CreateUser(user);
            MembershipUser mu = new MembershipUser("memProvider",
                                                   username, user.ID, email, "", "", true, true, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);

            status = MembershipCreateStatus.Success;
            return(mu);
        }
        public FloatMap GaussianBlur(FloatMap inMap, float sigma)
        {
            var      k      = _kernels["convolveImg"];
            FloatMap outMap = new FloatMap(inMap.W, inMap.H);

            FloatMap mask = createBlurMask(sigma);

            IMem <float> maskBuf = Cl.CreateBuffer <float>(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, mask._buf, out err);

            assert(err, "capholes mask, mem object creation");

            err = Cl.SetKernelArg(k, 2, intPtrSize, maskBuf);
            assert(err, "capholes mask, setKernelArg");

            err = Cl.SetKernelArg(k, 3, intSize, (mask.W - 1) / 2);
            assert(err, "capholes mask, setKernelArg");

            singlePass(k, inMap, outMap);

            return(outMap);
        }
        private FloatMap capHoles(FloatMap inMap, int filterHalfSize)
        {
            var      k      = _kernels["capHolesImg"];
            FloatMap outMap = new FloatMap(inMap.W, inMap.H);

            FloatMap mask = getDistanceWeightMap(filterHalfSize);

            IMem <float> maskBuf = Cl.CreateBuffer <float>(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, mask._buf, out err);

            assert(err, "capholes mask, mem object creation");

            err = Cl.SetKernelArg(k, 2, intPtrSize, maskBuf);
            assert(err, "capholes mask, setKernelArg");

            err = Cl.SetKernelArg(k, 3, intSize, filterHalfSize);
            assert(err, "capholes mask, setKernelArg");

            singlePass(k, inMap, outMap);

            Cl.ReleaseMemObject(maskBuf);

            return(outMap);
        }
        public WriteableBitmap GenerateSerialNxMWritableBitmap(IMem pixels, int loffset, int width, int height, int tileSizeX, int tileSizeY, BitmapPalette palette)
        {
            var wb = new WriteableBitmap(width, height, 96, 96, PixelFormats.Indexed8, palette);

            int tileLinearLength = tileSizeX * tileSizeY;
            int tilewidth        = width / tileSizeX;
            int tileheight       = height / tileSizeY;


            for (int tj = 0; tj < tileheight; tj++)
            {
                for (int ti = 0; ti < tilewidth; ti++)
                {
                    var left   = ti * tileSizeX;
                    var top    = tj * tileSizeY;
                    var offset = loffset + (ti * tileLinearLength) + (tj * tileLinearLength * tilewidth);
                    var arr    = pixels.readpixels(offset, tileLinearLength);

                    //// Reverse X
                    //var ar1 = arr.Select((x, idx) => new { x, idx })
                    //             .GroupBy(x => x.idx / tileSizeX)
                    //             .Select(x => x.Select(y => y.x).Reverse());



                    //// Reverse Y
                    //arr = ar1.SelectMany(x => x);

                    var ar3 = arr.Select(x => Convert.ToByte(x)).ToArray();
                    wb.WritePixels(new Int32Rect(left, top, tileSizeX, tileSizeY), ar3, tileSizeX, 0);
                }
            }


            return(wb);
        }
        private void ready()
        {
            ErrorCode error;

            context = Cl.CreateContext(null, 1, new[] { device }, null, IntPtr.Zero, out error);

            string source = System.IO.File.ReadAllText("kernels.cl");

            program = Cl.CreateProgramWithSource(context, 1, new[] { source }, null, out error);

            error = Cl.BuildProgram(program, 1, new[] { device }, string.Empty, null, IntPtr.Zero);
            InfoBuffer buildStatus = Cl.GetProgramBuildInfo(program, device, ProgramBuildInfo.Status, out error);

            if (buildStatus.CastTo <BuildStatus>() != BuildStatus.Success)
            {
                throw new Exception($"OpenCL could not build the kernel successfully: {buildStatus.CastTo<BuildStatus>()}");
            }
            allGood(error);

            Kernel[] kernels = Cl.CreateKernelsInProgram(program, out error);
            kernel = kernels[0];
            allGood(error);

            queue = Cl.CreateCommandQueue(context, device, CommandQueueProperties.None, out error);
            allGood(error);

            dataOut = Cl.CreateBuffer(context, MemFlags.WriteOnly, (IntPtr)(globalSize * sizeof(int)), out error);
            allGood(error);

            var intSizePtr = new IntPtr(Marshal.SizeOf(typeof(int)));

            error |= Cl.SetKernelArg(kernel, 2, new IntPtr(Marshal.SizeOf(typeof(IntPtr))), dataOut);
            error |= Cl.SetKernelArg(kernel, 3, intSizePtr, new IntPtr(worldSeed));
            error |= Cl.SetKernelArg(kernel, 4, intSizePtr, new IntPtr(globalSize));
            allGood(error);
        }
 public static void ReadFromBuffer <T>(this CommandQueue commandQueue, IMem <T> buffer, T[] array, int offset = 0, long length = -1, params Event[] waitFor)
     where T : struct
 {
     ReadFromBuffer(commandQueue, (IMem)buffer, array, offset, length, waitFor);
 }
Beispiel #31
0
        public void sortKeysOnly(IMem input, IMem output,
                    int numElements)
        {
            debugRead = new int[Math.Max(numElements,numCounters)];
            OpenCL.Net.ErrorCode error;
               Event eve;

               mCounters = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, gpuConstants.numGroupsPerBlock * gpuConstants.numRadices * gpuConstants.numBlocks * sizeof(int),
            out error);
               CheckErr(error, "Cl.CreateBuffer");

               mRadixPrefixes = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, gpuConstants.numRadices * sizeof(int), out error);
               CheckErr(error, "Cl.CreateBuffer");

               gpuConstants.numElementsPerGroup = (numElements/(gpuConstants.numBlocks*gpuConstants.numGroupsPerBlock)) +1 ;
               gpuConstants.numTotalElements = numElements;
               int i;
               for (i = 0; i < 8; i++)
               {
               error = Cl.EnqueueWriteBuffer(cqCommandQueue, mCounters, Bool.True, IntPtr.Zero, (IntPtr)(numCounters * 4),
                counters, 0, null, out eve);
               CheckErr(error, "Cl.EnqueueWriteBuffer Counter initialize");
               if (i%2 == 0)
               {
                   DateTime before = DateTime.Now;
                   SetupAndCount(input, 4 * i);
                   Console.WriteLine("Setup and Count =" + (DateTime.Now - before).TotalMilliseconds);

                   before = DateTime.Now;
                   SumIt(input, 4 * i);
                   Console.WriteLine("SumIt =" + (DateTime.Now - before).TotalMilliseconds);

                   before = DateTime.Now;
                   ReorderingKeysOnly(input, output, 4 * i);
                   Console.WriteLine("Reorder =" + (DateTime.Now - before).TotalMilliseconds);

               }
               else
               {
                   SetupAndCount(output, 4 * i);
                   SumIt(output, 4 * i);
                   ReorderingKeysOnly(output, input, 4 * i);
               }

               }
               if (i%2 != 0)
               {
               error= Cl.EnqueueCopyBuffer(cqCommandQueue, input, output, IntPtr.Zero, IntPtr.Zero, (IntPtr)(numElements * 4), 0, null, out eve);
               CheckErr(error, "Cl.EnqueueCopyBuffer");
               error = Cl.Finish(cqCommandQueue);
               CheckErr(error, "Cl.Finish Copybuffer");
               }
               error = Cl.ReleaseMemObject(mRadixPrefixes);
               CheckErr(error, "Cl.ReleaseMemObj");
               error = Cl.ReleaseMemObject(mCounters);
               CheckErr(error, "Cl.ReleaseMemObj");
        }
Beispiel #32
0
        public void sortKeysValue(IMem key, IMem value,  
              int numElements)
        {
            debugRead = new int[Math.Max(numElements, numCounters)];
               OpenCL.Net.ErrorCode error;
               Event eve;
               /*
                      error = Cl.EnqueueReadBuffer(cqCommandQueue, input, Bool.True, IntPtr.Zero, (IntPtr)(numElements * 4),
               debugRead, 0, null, out eve);
                      CheckErr(error, "Cl.EnqueueReadBuffer");
               */

               mCounters = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, gpuConstants.numGroupsPerBlock * gpuConstants.numRadices * gpuConstants.numBlocks * sizeof(int),
                out error);
               CheckErr(error, "Cl.CreateBuffer");

               mRadixPrefixes = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, gpuConstants.numRadices * sizeof(int), out error);
               CheckErr(error, "Cl.CreateBuffer");

               IMem outputKey = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)(4 * numElements),
                out error);
               CheckErr(error, "Cl.CreateBuffer");
               IMem outputValue = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)(8 * numElements),
            out error);
               CheckErr(error, "Cl.CreateBuffer");

               gpuConstants.numElementsPerGroup = (numElements / (gpuConstants.numBlocks * gpuConstants.numGroupsPerBlock)) + 1;
               gpuConstants.numTotalElements = numElements;
               int i;
               for (i = 0; i < 8; i++)
               {
               error = Cl.EnqueueWriteBuffer(cqCommandQueue, mCounters, Bool.True, IntPtr.Zero, (IntPtr)(numCounters * 4),
                counters, 0, null, out eve);
               CheckErr(error, "Cl.EnqueueWriteBuffer Counter initialize");
               if (i % 2 == 0)
               {
                   SetupAndCount(key, 4 * i);
                   SumIt(key, 4 * i);
                   ReorderingKeyValue(key, outputKey, value, outputValue, 4 * i);
               }
               else
               {
                   SetupAndCount(outputKey, 4 * i);
                   SumIt(outputKey, 4 * i);
                   ReorderingKeyValue(outputKey, key, outputValue, value, 4 * i);
               }

               }
               if (i % 2 == 0)
               {
               error = Cl.EnqueueCopyBuffer(cqCommandQueue, outputKey, key, IntPtr.Zero, IntPtr.Zero, (IntPtr)(numElements * 4), 0, null, out eve);
               CheckErr(error, "Cl.EnqueueCopyBuffer");
               error = Cl.Finish(cqCommandQueue);
               CheckErr(error, "Cl.Finish Copybuffer");
               error = Cl.EnqueueCopyBuffer(cqCommandQueue, outputValue, value, IntPtr.Zero, IntPtr.Zero, (IntPtr)(numElements * 8), 0, null, out eve);
               CheckErr(error, "Cl.EnqueueCopyBuffer");
               error = Cl.Finish(cqCommandQueue);
               CheckErr(error, "Cl.Finish Copybuffer");
               }

               error = Cl.ReleaseMemObject(outputKey);
               CheckErr(error, "Cl.ReleaseMemObj");
               error = Cl.ReleaseMemObject(outputValue);
               CheckErr(error, "Cl.ReleaseMemObj");
               error = Cl.ReleaseMemObject(mRadixPrefixes);
               CheckErr(error, "Cl.ReleaseMemObj");
               error = Cl.ReleaseMemObject(mCounters);
               CheckErr(error, "Cl.ReleaseMemObj");
        }
Beispiel #33
0
        private void ReorderingKeyValue(IMem inputKey, IMem outputKey,IMem inputValue, IMem outputValue, int bitOffset)
        {
            OpenCL.Net.ErrorCode error;
               IntPtr agentPtrSize = (IntPtr)0;
               agentPtrSize = (IntPtr)Marshal.SizeOf(typeof(IntPtr));
               var ptrSize = (IntPtr)Marshal.SizeOf(typeof(Mem));

               int globalWorkSize = gpuConstants.numThreadsPerBlock * gpuConstants.numBlocks;
               int localWorkSize = gpuConstants.numThreadsPerBlock;

               IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)globalWorkSize };
               IntPtr[] localWorkGroupSizePtr = new IntPtr[] { (IntPtr)localWorkSize };
               Event clevent;
               error = Cl.SetKernelArg(ckReorderingKeyValue, 0, ptrSize, inputKey);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckReorderingKeyValue, 1, ptrSize, outputKey);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckReorderingKeyValue, 2, ptrSize, inputValue);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckReorderingKeyValue, 3, ptrSize, outputValue);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckReorderingKeyValue, 4, ptrSize, mCounters);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckReorderingKeyValue, 5, ptrSize, mRadixPrefixes);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckReorderingKeyValue, 6, (IntPtr)(gpuConstants.numGroupsPerBlock * gpuConstants.numBlocks * gpuConstants.numRadicesPerBlock * 4), null);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckReorderingKeyValue, 7, (IntPtr)(Marshal.SizeOf(typeof(GPUConstants))), gpuConstants);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckReorderingKeyValue, 8, (IntPtr)4, bitOffset);
               CheckErr(error, "Cl.SetKernelArg");

               error = Cl.EnqueueNDRangeKernel(cqCommandQueue, ckReorderingKeyValue, 1, null, workGroupSizePtr, localWorkGroupSizePtr, 0, null, out clevent);
               CheckErr(error, "Cl.EnqueueNDRangeKernel");

               error = Cl.Finish(cqCommandQueue);
               CheckErr(error, "Cl.Finish");
               if (DEBUG)
               {
               Console.WriteLine("-------------------------------Reordering-------------------------------------------------");
               Event eve;

               Console.WriteLine("              Input                ");
               Cl.EnqueueReadBuffer(cqCommandQueue, inputKey, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0,
                   null, out eve);
               CheckErr(error, "Cl.EnqueueReadBuffer");
               PrintElementBuffer(debugRead, gpuConstants.numTotalElements, "Reordering -> Input -> bitoffset = " + bitOffset);

               Cl.EnqueueReadBuffer(cqCommandQueue, inputValue, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0,
                   null, out eve);
               CheckErr(error, "Cl.EnqueueReadBuffer");
               PrintElementBuffer(debugRead, gpuConstants.numTotalElements, "Reordering -> InputValues -> bitoffset = " + bitOffset);

               Console.WriteLine("              Counters                ");
               Cl.EnqueueReadBuffer(cqCommandQueue, mCounters, Bool.True, IntPtr.Zero, (IntPtr)(numCounters * sizeof(int)), debugRead, 0,
                   null, out eve);
               CheckErr(error, "Cl.EnqueueReadBuffer");
               PrintCounterBuffer(debugRead, "Reordering -> bitoffset = " + bitOffset);

               Console.WriteLine("              Counters                ");
               Cl.EnqueueReadBuffer(cqCommandQueue, mRadixPrefixes, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numRadices * sizeof(int)), debugRead, 0,
                   null, out eve);
               CheckErr(error, "Cl.EnqueueReadBuffer");
               PrintElementBuffer(debugRead, gpuConstants.numRadices, "Reordering -> RadixPrefixe -> bitoffset = " + bitOffset);

               Console.WriteLine("              Output                ");
               Cl.EnqueueReadBuffer(cqCommandQueue, outputKey, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0,
                   null, out eve);
               CheckErr(error, "Cl.EnqueueReadBuffer");
               PrintElementBuffer(debugRead, gpuConstants.numTotalElements, "Reordering -> Output -> bitoffset = " + bitOffset);

               Cl.EnqueueReadBuffer(cqCommandQueue, outputValue, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4), debugRead, 0,
                   null, out eve);
               CheckErr(error, "Cl.EnqueueReadBuffer");
               PrintElementBuffer(debugRead, gpuConstants.numTotalElements, "Reordering -> OutputValue -> bitoffset = " + bitOffset);

               Console.WriteLine("Reordering -> bitoffset = " + bitOffset);
               Console.WriteLine();
               };
        }
Beispiel #34
0
        private void SumIt(IMem input, int bitOffset)
        {
            OpenCL.Net.ErrorCode error;
               IntPtr agentPtrSize = (IntPtr)0;
               agentPtrSize = (IntPtr)Marshal.SizeOf(typeof(IntPtr));
               var ptrSize = (IntPtr)Marshal.SizeOf(typeof(Mem));

               int globalWorkSize = gpuConstants.numThreadsPerBlock * gpuConstants.numBlocks;
               int localWorkSize = gpuConstants.numThreadsPerBlock;

               IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)globalWorkSize };
               IntPtr[] localWorkGroupSizePtr = new IntPtr[] { (IntPtr)localWorkSize };
               Event clevent;
               error = Cl.SetKernelArg(ckSumIt, 0, ptrSize, input);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckSumIt, 1, ptrSize, mCounters);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckSumIt, 2, ptrSize, mRadixPrefixes);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckSumIt, 3, (IntPtr)(Marshal.SizeOf(typeof(GPUConstants))), gpuConstants);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckSumIt, 4, (IntPtr)4, bitOffset);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.SetKernelArg(ckSumIt, 5, (IntPtr)(4* gpuConstants.numBlocks*gpuConstants.numGroupsPerBlock), null);
               CheckErr(error, "Cl.SetKernelArg");
               error = Cl.EnqueueNDRangeKernel(cqCommandQueue, ckSumIt, 1, null, workGroupSizePtr, localWorkGroupSizePtr, 0, null, out clevent);
               CheckErr(error, "Cl.EnqueueNDRangeKernel");

               error = Cl.Finish(cqCommandQueue);
               CheckErr(error, "Cl.Finish");
               if (DEBUG)
               {
               Event eve;

               error = Cl.EnqueueReadBuffer(cqCommandQueue, input, Bool.True, IntPtr.Zero, (IntPtr)(gpuConstants.numTotalElements * 4),
                debugRead, 0, null, out eve);
                CheckErr(error, "Cl.EnqueueReadBuffer");

                PrintElementBuffer(debugRead, gpuConstants.numTotalElements, "SumIt -> Input -> bitoffset = " + bitOffset);

               Console.WriteLine("              Counters                ");
               Cl.EnqueueReadBuffer(cqCommandQueue, mCounters, Bool.True, IntPtr.Zero, (IntPtr)(numCounters * sizeof(int)), debugRead, 0,
                   null, out eve);
               CheckErr(error, "Cl.EnqueueReadBuffer");
               PrintCounterBuffer(debugRead, "SumIt -> bitoffset = " + bitOffset);
               Console.WriteLine("SumIt -> bitoffset = " + bitOffset);

               Console.WriteLine();
               };
        }
Beispiel #35
0
		public static ErrorCode EnqueueReleaseGLObjects(CommandQueue queue, IMem[] glObjects, uint waitListCount, OpenCL.Net.Event[] waitList, out OpenCL.Net.Event outEvent){
			return clEnqueueReleaseGLObjects (queue, (uint)glObjects.Length, (from m in glObjects select (m as IHandleData).Handle).ToArray (), waitListCount, waitList, out outEvent);
		}
Beispiel #36
0
        private void InitOpenCl()
        {
            if (File.Exists(Path.Combine(System.Environment.CurrentDirectory, ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "log.txt")))
            {
                File.Delete(Path.Combine(System.Environment.CurrentDirectory, ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "log.txt"));
            }

            if (File.Exists(Path.Combine(System.Environment.CurrentDirectory, ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "sortLog.txt")))
            {
                File.Delete(Path.Combine(System.Environment.CurrentDirectory, ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "sortLog.txt"));
            }
            if (File.Exists(Path.Combine(System.Environment.CurrentDirectory, ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "OpenCLDebugLog.txt")))
            {
                File.Delete(Path.Combine(System.Environment.CurrentDirectory, ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "OpenCLDebugLog.txt"));
            }

            OpenCL.Net.ErrorCode error;
            var platforms = Cl.GetPlatformIDs(out error);
            List<Device> devicesList = new List<Device>();

            CheckErr(error, "Cl.GetPlatformIDs");

            foreach (Platform platform in platforms)
            {
                string platformName = Cl.GetPlatformInfo(platform, PlatformInfo.Name, out error).ToString();
                Console.WriteLine("Platform: " + platformName);
                CheckErr(error, "Cl.GetPlatformInfo");
                //We will be looking only for GPU devices
                foreach (Device device in Cl.GetDeviceIDs(platform, DeviceType.Gpu, out error))
                {
                    CheckErr(error, "Cl.GetDeviceIDs");

                    var vendor = Cl.GetDeviceInfo(device, DeviceInfo.Vendor, out error);
                    var name = Cl.GetDeviceInfo(device, DeviceInfo.Name, out error);
                    var worksize = Cl.GetDeviceInfo(device, DeviceInfo.MaxWorkGroupSize, out error);
                    Console.WriteLine("Vendor: " + vendor + " , " + name);

                    Console.WriteLine("Device: " + device.GetType());
                    Console.WriteLine("Workgroupsize: " + worksize.CastTo<long>());
                    devicesList.Add(device);
                }
            }

            if (devicesList.Count <= 0)
            {
                Console.WriteLine("No devices found.");
                return;
            }

            _device = devicesList[0];

            if (Cl.GetDeviceInfo(_device, DeviceInfo.ImageSupport, out
                error).CastTo<Bool>() == Bool.False)
            {
                Console.WriteLine("No image support.");
                return;
            }
            cxGPUContext
                = Cl.CreateContext(null, 1, new[] { _device }, ContextNotify,
                    IntPtr.Zero, out error); //Second parameter is amount of devices
            CheckErr(error, "Cl.CreateContext");

            cqCommandQueue = Cl.CreateCommandQueue(cxGPUContext, _device, (CommandQueueProperties)0, out error);
            CheckErr(error, "Cl.CreateCommandQueue");

            sort = new GPURadixSort(cqCommandQueue, cxGPUContext, _device);

            constants.numBlocks = numBlocks;
            constants.numThreadsPerBlock = numThreadsPerBlock;
            constants.xMax = (float)m_OuterBoundary.X;
            constants.yMax = (float)m_OuterBoundary.Y;
            constants.zMax = (float)m_OuterBoundary.Z;
            constants.cellSizeX = (float)m_CellSize.X;
            constants.cellSizeY = (float)m_CellSize.Y;
            constants.cellSizeZ = (float)m_CellSize.Z;
            constants.xGridBoundary = (int)(m_OuterBoundary.X / m_CellSize.X);
            constants.yGridBoundary = (int)(m_OuterBoundary.Y / m_CellSize.Y);
            constants.zGridBoundary = (int)(m_OuterBoundary.Z / m_CellSize.Z);

            string programSource = System.IO.File.ReadAllText(Path.Combine(System.Environment.CurrentDirectory, ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "CollisionDetection.cl"));
            IntPtr[] progSize = new IntPtr[] { (IntPtr)programSource.Length };
            OpenCL.Net.Program clProgramCollision = Cl.CreateProgramWithSource(cxGPUContext, 1, new[] { programSource }, progSize,
                out error);
            CheckErr(error, "createProgramm");
            string flags = "-cl-fast-relaxed-math";

            error = Cl.BuildProgram(clProgramCollision, 1, new[] { _device }, flags, null, IntPtr.Zero);
            CheckErr(error, "Cl.BuildProgram");
            //Check for any compilation errors
            if (Cl.GetProgramBuildInfo(clProgramCollision, _device, ProgramBuildInfo.Status, out error).CastTo<BuildStatus>()
                != BuildStatus.Success)
            {
                CheckErr(error, "Cl.GetProgramBuildInfo");
                Console.WriteLine("Cl.GetProgramBuildInfo != Success");
                Console.WriteLine(Cl.GetProgramBuildInfo(clProgramCollision, _device, ProgramBuildInfo.Log, out error));
                using (StreamWriter sw = File.AppendText(Path.Combine(System.Environment.CurrentDirectory, ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "OpenCLDebugLog.txt")))
                {
                    sw.WriteLine(Cl.GetProgramBuildInfo(clProgramCollision, _device, ProgramBuildInfo.Log, out error));
                }
                return;
            }
            int ciErrNum;

            ckCreateCellIdArray = Cl.CreateKernel(clProgramCollision, "CreateCellId", out error);
            CheckErr(error, "Cl.CreateKernel");
            ckCreateCollisionList = Cl.CreateKernel(clProgramCollision, "CreateCollisionList", out error);
            CheckErr(error, "Cl.CreateKernel");
            ckCreateCollsionTuples = Cl.CreateKernel(clProgramCollision, "CreateCollsionTuples", out error);
            CheckErr(error, "Cl.CreateKernel");
            ckCheckCollsions = Cl.CreateKernel(clProgramCollision, "CheckCollsions", out error);
            CheckErr(error, "Cl.CreateKernel");
            freeCellList = new bool[constants.xGridBoundary*constants.yGridBoundary*constants.zGridBoundary];

            freeCellMem = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)(freeCellList.Length *Marshal.SizeOf(typeof(bool))), sharedIdxMem,
                out error);
            CheckErr(error, "Createbuffer");
        }
Beispiel #37
0
        private void CheckCollsions()
        {
            ErrorCode error;
            Event eve;
            IntPtr agentPtrSize = (IntPtr)0;
            var ptrSize = (IntPtr)Marshal.SizeOf(typeof(Mem));

            int globalWorkSize = constants.numThreadsPerBlock * constants.numBlocks;
            int localWorkSize = constants.numThreadsPerBlock;

            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)globalWorkSize };
            IntPtr[] localWorkGroupSizePtr = new IntPtr[] { (IntPtr)localWorkSize };
            Event clevent;
            collisons = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)(sharedIdx[0] * Marshal.SizeOf(typeof(CollisionTupel))),
            out error);
            CheckErr(error, "Createbuffer");

            collisionShapes = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)(sharedIdx[0] * Marshal.SizeOf(typeof(ShapeTupel))),
              out error);
            CheckErr(error, "Createbuffer");
            collisionShapeTupels = new ShapeTupel[sharedIdx[0]];

            for (int i = 0; i < sharedIdx[0]; i++)
            {
                collisionShapeTupels[i] = new ShapeTupel();
                int keytmp = (int)(readCellData[tupelList[i].obj1] & 0xFFFFFFFF);

                collisionShapeTupels[i].obj1 = objIdClShapeMap[keytmp];

                keytmp = (int)(readCellData[tupelList[i].obj2] & 0xFFFFFFFF);

                collisionShapeTupels[i].obj2 = objIdClShapeMap[keytmp];
            }
            // CheckCollsions
            error = Cl.EnqueueWriteBuffer(cqCommandQueue, sharedIdxMem, Bool.True, IntPtr.Zero, (IntPtr)(4),
                     0, 0, null, out eve);
            CheckErr(error, "EnqBuffer");
            error = Cl.EnqueueWriteBuffer(cqCommandQueue, collisionShapes, Bool.True, IntPtr.Zero, (IntPtr)(sharedIdx[0] * Marshal.SizeOf(typeof(ShapeTupel))),
                                 collisionShapeTupels, 0, null, out eve);

            CheckErr(error, "EnqBuffer");
            error = Cl.SetKernelArg(ckCheckCollsions, 0, ptrSize, cellData);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCheckCollsions, 1, ptrSize, collisionTuples);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCheckCollsions, 2, ptrSize, collisionShapes);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCheckCollsions, 3, ptrSize, collisons);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCheckCollsions, 4, ptrSize, sharedIdxMem);

            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCheckCollsions, 5, (IntPtr)(Marshal.SizeOf(typeof(CollisionConstants))), constants);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCheckCollsions, 6, (IntPtr)4, sharedIdx[0]);
            CheckErr(error, "Cl.SetKernelArg");

            error = Cl.EnqueueNDRangeKernel(cqCommandQueue, ckCheckCollsions, 1, null, workGroupSizePtr, localWorkGroupSizePtr, 0, null, out clevent);
            CheckErr(error, "Cl.EnqueueNDRangeKernel");

            error = Cl.Finish(cqCommandQueue);
            CheckErr(error, "Cl.Finish");

            error = Cl.EnqueueReadBuffer(cqCommandQueue, sharedIdxMem, Bool.True, IntPtr.Zero, (IntPtr)(4),
                 sharedIdx, 0, null, out eve);
            CheckErr(error, "Cl.EnqueueReadBuffer");

            tupelList = new CollisionTupel[sharedIdx[0]];
            error = Cl.EnqueueReadBuffer(cqCommandQueue, collisons, Bool.True, IntPtr.Zero, (IntPtr)(sharedIdx[0] * Marshal.SizeOf(typeof(CollisionTupel))),
            tupelList, 0, null, out eve);

            CheckErr(error, "Cl.EnqueueReadBuffer");
        }
Beispiel #38
0
        private void CreateBuffers()
        {
            ErrorCode error;

            Event eve;
            collisionList = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)( constants.numTotalElements * 4 * 8),
                   out error);

            // Create Buffers
            shapeMem = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)( constants.numTotalElements * Marshal.SizeOf(typeof(clShapeObject))), clShapeArray.ToArray(),
                 out error);
            CheckErr(error, "Createbuffer");
            sharedIdxMem = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)(4), sharedIdxMem,
                out error);
            CheckErr(error, "Createbuffer");
            objIdMem = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)(clObjArray.Count * 8),
                out error);
            CheckErr(error, "Createbuffer");
            cellIds = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)(clObjArray.Count * 4 * 8),
                out error);
            CheckErr(error, "Createbuffer");
            cellData = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)(clObjArray.Count * 8 * 8),
                out error);
            CheckErr(error, "Createbuffer");
            if (DEBUG)
            {
                DebugCollisionPosList = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)( constants.numTotalElements * 8 * Marshal.SizeOf(typeof(CollisionCell))),
                    out error);
            }

            error = Cl.EnqueueWriteBuffer(cqCommandQueue, shapeMem, Bool.True, IntPtr.Zero, (IntPtr)(clShapeArray.Count * Marshal.SizeOf(typeof(clShapeObject))),
                clShapeArray.ToArray(), 0, null,
                out eve);
            CheckErr(error, "EnqBuffer");

            error = Cl.EnqueueWriteBuffer(cqCommandQueue, objIdMem, Bool.True, IntPtr.Zero, (IntPtr)(clObjArray.Count * 8),
                clObjArray.ToArray(), 0, null,
                out eve);
            CheckErr(error, "EnqBuffer");

            error = Cl.EnqueueWriteBuffer(cqCommandQueue, sharedIdxMem, Bool.True, IntPtr.Zero, (IntPtr)(4),
                0, 0, null,
                out eve);
            CheckErr(error, "EnqBuffer");

            /* error = Cl.EnqueueWriteBuffer(cqCommandQueue, freeCellMem, Bool.True, IntPtr.Zero,(IntPtr)(freeCellList.Length * Marshal.SizeOf(typeof(bool))),
                 freeCellList, 0, null,
                 out eve);
             CheckErr(error, "EnqBuffer");*/

            error = Cl.Finish(cqCommandQueue);
            CheckErr(error, "Cl.Finish");
        }
Beispiel #39
0
        private void CreateCollsionTuples()
        {
            ErrorCode error;
            Event eve;
            IntPtr agentPtrSize = (IntPtr)0;
            var ptrSize = (IntPtr)Marshal.SizeOf(typeof(Mem));

            int globalWorkSize = constants.numThreadsPerBlock * constants.numBlocks;
            int localWorkSize = constants.numThreadsPerBlock;

            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)globalWorkSize };
            IntPtr[] localWorkGroupSizePtr = new IntPtr[] { (IntPtr)localWorkSize };
            Event clevent;
            collisionTuples = Cl.CreateBuffer(cxGPUContext, MemFlags.ReadWrite, (IntPtr)( constants.numTotalElements * 8 * Marshal.SizeOf(typeof(CollisionTupel))),
                out error);
            CheckErr(error, "Createbuffer");

            error = Cl.EnqueueWriteBuffer(cqCommandQueue, sharedIdxMem, Bool.True, IntPtr.Zero, (IntPtr)(4),
                 0, 0, null, out eve);
            Console.WriteLine(" Num Elements {0} total {1}", sharedIdx[0], constants.numTotalElements);
            CheckErr(error, "EnqBuffer");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 0, ptrSize, cellData);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 1, ptrSize, collisionList);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 2, ptrSize, collisionTuples);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 3, ptrSize, sharedIdxMem);

            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 4, (IntPtr)(Marshal.SizeOf(typeof(CollisionConstants))), constants);
            CheckErr(error, "Cl.SetKernelArg");
            error = Cl.SetKernelArg(ckCreateCollsionTuples, 5, (IntPtr)4, sharedIdx[0]);
            CheckErr(error, "Cl.SetKernelArg");

            error = Cl.EnqueueNDRangeKernel(cqCommandQueue, ckCreateCollsionTuples, 1, null, workGroupSizePtr, localWorkGroupSizePtr, 0, null, out clevent);
            CheckErr(error, "Cl.EnqueueNDRangeKernel");

            error = Cl.Finish(cqCommandQueue);
            CheckErr(error, "Cl.Finish");

            error = Cl.EnqueueReadBuffer(cqCommandQueue, sharedIdxMem, Bool.True, IntPtr.Zero, (IntPtr)(4),
                sharedIdx, 0, null, out eve);
            CheckErr(error, "Cl.EnqueueReadBuffer");

            error = Cl.EnqueueReadBuffer(cqCommandQueue, collisionTuples, Bool.True, IntPtr.Zero, (IntPtr)(sharedIdx[0] * Marshal.SizeOf(typeof(CollisionTupel))),
            tupelList, 0, null, out eve);
            CheckErr(error, "Cl.EnqueueReadBuffer");

                for (int i = 0; i < readCellIdList.Count(); i++)
                {
                    if (cellMap.ContainsKey(readCellIdList[i]))
                    {
                        cellPosList[i] = cellMap[readCellIdList[i]];
                    }
                    else
                    {
                        cellPosList[i] = new CollisionCell();
                    }
                }
                if (DEBUG)
                {
                DebugHelper.PrintCollisionTuples(tupelList, readCellIdList, readCellData, cellPosList, "Collision tuples to check", sharedIdx[0]);
            }

            error = Cl.ReleaseMemObject(collisionList);
            CheckErr(error, "Cl.ReleaseMem");
        }