Ejemplo n.º 1
0
        public void CanComposeFunction()
        {
            Device   device = new Device(testContext);
            string   code   = @"function (I) -> (O) {
                                O = I * I;
                            }";
            Function f      = new Function(testContext, code);

            Assert.True(f.IsAllocated);
            DeviceTensor i = new DeviceTensor(device, new Compiler.PlaidML.Shape(testContext, PlaidmlDatatype.PLAIDML_DATA_INT32, 6),
                                              "I");
            DeviceTensor o = new DeviceTensor(device, new Compiler.PlaidML.Shape(testContext, PlaidmlDatatype.PLAIDML_DATA_INT32, 6),
                                              "O");

            Int32[] input_data = { 0, 1, 3, 4, 5, 6 };
            i.CreateView <Int32>(MemoryMapType.Discard).CopyFromAndFree(input_data);
            var v = i.CreateView <Int32>(MemoryMapType.Retain);

            Assert.Equal(3, v[2]);
            Invoker <int> inv1 = new Invoker <int>(testContext, f, o, i);

            Assert.True(inv1.IsAllocated);
            Invocation <int> k = inv1.Invoke();

            Assert.True(k.IsAllocated);
            DeviceTensorView <Int32> R = o.CreateView <Int32>(MemoryMapType.Retain);

            Assert.Equal(6, R.ElementCount);
            Assert.Equal(9, R[2]);


            DeviceTensor co = new DeviceTensor(device, new Compiler.PlaidML.Shape(testContext, PlaidmlDatatype.PLAIDML_DATA_INT32, 6),
                                               "O");
            Composer c = new Composer(testContext);
            Applier  a = new Applier(testContext, f);

            a.AddInputValue(i);
            var fo = a.AddOutputValue("O");

            Assert.True(fo.IsAllocated);
            Assert.True(c.AddInputPlaceholder("I", i.DimensionCount));
            Assert.True(c.AddOutputValue(co));
            Assert.True(c.AddDependency(a));
            Assert.True(c.AddUpdate(co, fo));
            Function gf = c.BuildFunction();

            Assert.True(gf.IsAllocated);
            Invoker <int> gi = new Invoker <int>(testContext, gf, co, i);

            Assert.True(gi.IsAllocated);

            Invocation <int> inv = gi.Invoke();

            Assert.True(inv.IsAllocated);
            R = co.CreateView <Int32>(MemoryMapType.Retain);
            Assert.Equal(6, R.ElementCount);
            Assert.Equal(9, R[2]);
        }