Ejemplo n.º 1
0
        private static void DoInvoke(int[] WorkSize, object Target, HlGraphEntry CacheEntry, InvokeContext ctx, OpenCLNet.Device device)
        {
            HighLevel.HlGraph HLgraph = CacheEntry.HlGraph;

            foreach (KeyValuePair <FieldInfo, HighLevel.ArgumentLocation> Entry in HLgraph.StaticFieldMap)
            {
                ctx.PutArgument(Entry.Value, Entry.Key.GetValue(null));
            }

            SetArguments(ctx, Target, HLgraph.RootPathEntry);

            /*
             * foreach (KeyValuePair<FieldInfo, HighLevel.ArgumentLocation> Entry in HLgraph.ThisFieldMap)
             * {
             *  ctx.PutArgument(Entry.Value, Entry.Key.GetValue(Target));
             * }
             * foreach (KeyValuePair<FieldInfo, Dictionary<FieldInfo, HighLevel.ArgumentLocation>> Entry in HLgraph.OuterThisFieldMap) {
             *  object RealThis = Entry.Key.GetValue(Target);
             *  foreach (KeyValuePair<FieldInfo, HighLevel.ArgumentLocation> SubEntry in Entry.Value) {
             *      ctx.PutArgument(SubEntry.Value, SubEntry.Key.GetValue(RealThis));
             *  }
             * }*/

            foreach (KeyValuePair <HighLevel.ArgumentLocation, HighLevel.ArrayInfo> Entry in HLgraph.MultiDimensionalArrayInfo)
            {
                System.Diagnostics.Debug.Assert(Entry.Key.Index >= 0 && Entry.Key.Index < ctx.Arguments.Count);
                InvokeArgument BaseArrayArg = ctx.Arguments[Entry.Key.Index];
                System.Diagnostics.Debug.Assert(BaseArrayArg != null && BaseArrayArg.Value != null && BaseArrayArg.Value.GetType() == Entry.Key.DataType);
                System.Diagnostics.Debug.Assert(Entry.Key.DataType.IsArray && Entry.Key.DataType.GetArrayRank() == Entry.Value.DimensionCount);
                System.Diagnostics.Debug.Assert(BaseArrayArg.Value is Array);

                Array BaseArray  = (System.Array)BaseArrayArg.Value;
                long  BaseFactor = 1;
                for (int Dimension = 1; Dimension < Entry.Value.DimensionCount; Dimension++)
                {
                    int ThisDimensionLength = BaseArray.GetLength(Entry.Value.DimensionCount - 1 - (Dimension - 1));
                    BaseFactor *= ThisDimensionLength;
                    ctx.PutArgument(Entry.Value.ScaleArgument[Dimension], (int)BaseFactor);
                }
            }
            ctx.Complete();

            OpenCLInterop.CallOpenCLNet(WorkSize, CacheEntry, ctx, HLgraph, device);
        }
Ejemplo n.º 2
0
        public void PutArgument(HighLevel.ArgumentLocation Location, object Value)
        {
            int Index = Location.Index;

            System.Diagnostics.Debug.Assert(Index >= 0 && Index < m_Arguments.Count);
            System.Diagnostics.Debug.Assert(m_Arguments[Index] == null);

            InvokeArgument Argument = null;

            if (object.ReferenceEquals(Value, null))
            {
                Argument = new InvokeArgument.Int32Arg(0);
            }
            else if (Value is int)
            {
                Argument = new InvokeArgument.Int32Arg((int)Value);
            }
            else if (Value is uint)
            {
                Argument = new InvokeArgument.UInt32Arg((uint)Value);
            }
            else if (Value is long)
            {
                Argument = new InvokeArgument.Int64Arg((long)Value);
            }
            else if (Value is ulong)
            {
                Argument = new InvokeArgument.UInt64Arg((ulong)Value);
            }
            else if (Value is float)
            {
                Argument = new InvokeArgument.FloatArg((float)Value);
            }
            else if (Value is double)
            {
                Argument = new InvokeArgument.DoubleArg((double)Value);
            }
            else
            {
                Type Type = Value.GetType();
                if (Type.IsArray)
                {
                    bool ForRead = false, ForWrite = false;
                    if ((Location.Flags & HighLevel.LocationFlags.IndirectRead) != 0)
                    {
                        ForRead = true;
                    }
                    if ((Location.Flags & HighLevel.LocationFlags.IndirectWrite) != 0)
                    {
                        ForWrite = true;
                    }

                    if (!ForRead && !ForWrite)
                    {
                        ForRead = ForWrite = true;
                    }

                    Type ElementType = Type.GetElementType();
                    if (ElementType == typeof(byte) || ElementType == typeof(int) || ElementType == typeof(uint) || ElementType == typeof(long) || ElementType == typeof(ulong) ||
                        ElementType == typeof(float) || ElementType == typeof(double))
                    {
                        Argument = new InvokeArgument.PrimitiveArrayArg((System.Array)Value, ForRead, ForWrite);
                    }
                    else if (ValueTypeMap.ContainsKey(ElementType))
                    {
                        Argument = new InvokeArgument.MarshalledArrayArg((System.Array)Value, ForRead, ForWrite);
                    }
                }
            }

            if (Argument == null)
            {
                throw new InvalidOperationException(string.Format("Sorry, argument type '{0}' cannot be marshalled for OpenCL.", Value.GetType()));
            }

            m_Arguments[Index] = Argument;
        }