public float CalculateVar(out double totalMs, float cf = .9f, int iterations = 10)
        {
            using (var annualizedReturnsKernel = _env.Context.CompileKernelFromSource(_programSource, "annualizedReturns"))
                using (var aggregateReturnsKernel = _env.Context.CompileKernelFromSource(_programSource, "aggregateReturnsKernel"))
                {
                    Event clStart, clEnd;
                    var   queue = _env.CommandQueues[0];

                    var xSize = (_stockData.QuotesPerStock - _holding);
                    var ySize = _stockData.StocksCount;

                    var err = Cl.SetKernelArg(annualizedReturnsKernel, 0, _d_output);
                    err = Cl.SetKernelArg(annualizedReturnsKernel, 1, _d_stocksAndPrices);
                    err = Cl.SetKernelArg(annualizedReturnsKernel, 2, _d_portfolioStockMv);
                    err = Cl.SetKernelArg(annualizedReturnsKernel, 3, _ann);
                    err = Cl.SetKernelArg(annualizedReturnsKernel, 4, _holding);

                    err = Cl.SetKernelArg(aggregateReturnsKernel, 0, _d_output);
                    err = Cl.SetKernelArg(aggregateReturnsKernel, 1, ySize);

                    Cl.EnqueueMarker(queue, out clStart);
                    Cl.Finish(queue);

                    for (int i = 0; i < iterations; i++)
                    {
                        err = Cl.EnqueueNDRangeKernel(queue, annualizedReturnsKernel, 2, null,
                                                      new[] { (IntPtr)xSize, (IntPtr)ySize },
                                                      new[] { (IntPtr)256, (IntPtr)1 },
                                                      0, null, out Event notInterestedInThisEvent1);

                        err = Cl.EnqueueNDRangeKernel(queue, aggregateReturnsKernel, 2, null,
                                                      new[] { (IntPtr)xSize, (IntPtr)1 },
                                                      new[] { (IntPtr)256, (IntPtr)1 },
                                                      0, null, out notInterestedInThisEvent1);
                    }

                    Cl.EnqueueMarker(queue, out clEnd);
                    Cl.Finish(queue);

                    var startInfo = Cl.GetEventProfilingInfo(clStart, ProfilingInfo.Start, out err);
                    var endInfo   = Cl.GetEventProfilingInfo(clEnd, ProfilingInfo.End, out err);

                    Cl.EnqueueReadBuffer <float>(queue, _d_output, Bool.True, _h_output, 0, null, out Event notInterestedInThisEvent2);

                    totalMs = (endInfo.CastTo <ulong>() - startInfo.CastTo <ulong>()) * 10e-6;

                    clStart.Dispose();
                    clEnd.Dispose();
                    annualizedReturnsKernel.Dispose();

                    return(_h_output[0] + _h_output[xSize - 1]);
                }
        }
Example #2
0
        public Event EnqueueMarker()
        {
            ClHelper.ThrowNullException(Handle);

            unsafe
            {
                IntPtr event_ptr = IntPtr.Zero;

                ClHelper.GetError(Cl.EnqueueMarker(Handle, &event_ptr));

                return(new Event(event_ptr));
            }
        }