Exemplo n.º 1
0
        internal Exchange(XPU xpu, MemoryFragment f, bool isCopy = false)
        {
            XPU = xpu;

            int tid = 0;
            int pos = 0;

            pos     = f.Read(ref tid, pos);
            IsValid = tid == EXCHANGE_TYPE_ID;

            if (IsValid)
            {
                Fragment = f;

                pos        = f.Read(ref ID, pos);
                pos        = f.Read(ref RefID, pos);
                pos        = f.Read(ref Flags, pos);
                pos        = f.Read(ref Created, pos);
                pos        = f.Read(ref ErrorCode, pos);
                pos        = f.Read(ref ResIDLen, pos);
                ResID      = Encoding.UTF8.GetString(f.Span().Slice(pos, ResIDLen));
                DataOffset = pos + ResIDLen;
                state      = isCopy ? (int)XState.Created : (int)XState.Received;
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a stream objects.
 /// </summary>
 /// <param name="frag"></param>
 /// <exception cref="ArgumentNullException">If frag is null.</exception>
 /// <exception cref="ObjectDisposedException">If the fragment is disposed.</exception>
 public FragmentStream(MemoryFragment frag)
 {
     if (frag.IsDisposed)
     {
         throw new ObjectDisposedException("frag");
     }
     Fragment = frag ?? throw new ArgumentNullException("frag");
     len      = frag.Length;
 }
Exemplo n.º 3
0
 public static bool SearchEqualMemory(MemoryFragment input)
 {
     for (int i = 0; i < memoryFragments.Count; i++)
     {
         if (CheckInput(memoryFragments[i], input) && input.output == memoryFragments[i].output)
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 4
0
    public static int ReasoningMemory(MemoryFragment experience, MemoryFragment moment)
    {
        if (CheckInput(experience, moment))
        {
            if (experience.result) //jika benar lakukan aja
            {
                Debug.Log("I know where i must jumped to, because I experienced it : " + experience);
                return(experience.output);
            }
            else   //jika pengalman salah, harus diperbaiki, setidaknya satu langkah kedepan
            {
                //return experience.output == 1 ? 0 : 1; //Abaikan ini
                //experience.output = (1 + experience.output) % experience.input.Length;
                //return experience.output;

                Debug.Log("Hmm.. I ever fall but i don't know where i must go");

                //Mencari yang benar
                bool[] isExists = new bool[GetInputCount()];
                for (int i = 0; i < memoryFragments.Count; i++)
                {
                    if (CheckInput(memoryFragments[i], experience))//Ada yang sama
                    {
                        isExists[experience.output] = true;
                        if (memoryFragments[i].result)
                        {
                            Debug.Log("Ahh I know it");
                            return(memoryFragments[i].output);
                        }
                    }
                }

                Debug.Log("Maybe i can guess the jumped i never do");

                //Tidak ada yang benar, mencari yang belum pernah diambil
                for (int i = 0; i < isExists.Length; i++)
                {
                    if (!isExists[i])
                    {
                        return(i); //Return output yang belum pernah diambil
                    }
                }

                Debug.Log("Well, idk. Just let me die, so I experienced more.");
                //Sama sekali nggak ada
                return(experience.output = (1 + experience.output) % GetInputCount());
            }
        }
        Debug.Log("Memory not found, I must think further");
        return(-1);
    }
Exemplo n.º 5
0
    public static bool CheckInput(MemoryFragment exp, MemoryFragment mom)
    {
        bool[] experience = exp.input;
        bool[] moment     = mom.input;

        for (int j = 0; j < experience.Length; j++)
        {
            if (experience[j] != moment[j])
            {
                //Debug.Log("NOT EQUAL");
                return(false); //Not Equal
            }
        }
        Debug.Log("EQUAL" + exp + mom);
        return(true); //Equal
    }
Exemplo n.º 6
0
    public static int SummarizeMemory(bool[] input)
    {
        MemoryFragment mfnew = new MemoryFragment(input, -1, false);

        foreach (MemoryFragment mf in memoryFragments)
        {
            int reason = ReasoningMemory(mf, mfnew);
            if (reason == -1)
            {
            }
            else
            {
                return(reason);
            }
        }
        Debug.Log("Because I'm confused, I jumped randomly 2");
        return(Random.Range(0, GetInputCount()));
    }
Exemplo n.º 7
0
        public Block(int id, ushort tileSize, MemoryFragment frag)
        {
            ID         = id;
            fragment   = frag;
            TileSize   = tileSize;
            TotalSize  = frag.Length;
            TilesCount = frag.Length / tileSize;

            if (TotalSize > tileSize && frag.Length % tileSize != 0)
            {
                TilesCount++;
            }
            if (TilesCount == 0)
            {
                TilesCount = 1;
            }

            beamLoop = new TaskCompletionSource <bool>();
            tileMap  = new BitMask(TilesCount);
        }
Exemplo n.º 8
0
    public static void AddMemory(bool[] input, int output, bool result)
    {
        MemoryFragment mf = new MemoryFragment(input, output, result);

        if (SearchEqualMemory(mf) == false)
        {
            memoryFragments.Add(mf);
            Debug.Log("Okay, I got new memory : " + memoryFragments[memoryFragments.Count - 1]);
        }
        else
        {
            Debug.Log("Memory is exists, if I recall it again : " +
                      mf); //Useless Memory
        }

        Debug.Log("REASONGING SIZE " + memoryFragments.Count);
        for (int k = 0; k < memoryFragments.Count; k++)
        {
            Debug.Log("REASONING : " + memoryFragments[k]);
        }
    }
Exemplo n.º 9
0
        public static T Deserialize <T>(this MemoryFragment f, int from = 0)
        {
            if (f == null || f.IsDisposed)
            {
                throw new ArgumentNullException("f");
            }

            object o = null;

            using (var fs = f.CreateStream())
            {
                if (from > 0)
                {
                    fs.Seek(from, SeekOrigin.Begin);
                }

                var bf = new BinaryFormatter();
                o = bf.Deserialize(fs);

                return((T)o);
            }
        }
 private void Awake()
 {
     memoryFragment = GetComponent <MemoryFragment>();
 }
Exemplo n.º 11
0
        public async Task Start(IDictionary <string, List <string> > args)
        {
            await Task.Delay(0);

            if (args.ContainsKey("+all"))
            {
                args.Add("-store", new List <string>()
                {
                    "mh", "mmf", "nh", "vh"
                });
            }

            var opt = args["-store"];

            opt.AssertNothingOutsideThese("mh", "mmf", "nh", "vh");

            var stg = new HighwaySettings(1024, 2);
            var iH  = new Dictionary <string, IMemoryHighway>();

            var LEN = stg.DefaultCapacity;

            iH.Add("mh", new HeapHighway(stg, LEN, LEN));
            iH.Add("nh", new MarshalHighway(stg, LEN, LEN));
            iH.Add("mmf", new MappedHighway(stg, LEN, LEN));
            iH.Add("vh", new VirtualHighway());

            foreach (var kp in iH)
            {
                var hwName = kp.Value.GetType().Name;

                if (opt.Contains(kp.Key))
                {
                    var hw = kp.Value;
                    using (hw)
                    {
                        var F = new MemoryFragment[] {
                            hw.AllocFragment(200),
                            hw.AllocFragment(200)
                        };

                        foreach (var f in F)
                        {
                            var p = 0;

                            bool     b  = true;
                            int      i  = 10;
                            double   d  = 2.2;
                            DateTime dt = DateTime.Now;
                            char     c  = 'c';
                            Guid     g  = Guid.NewGuid();
                            byte[]   ba = new byte[3] {
                                1, 2, 3
                            };

                            p = f.Write(b, p);
                            p = f.Write(c, p);
                            p = f.Write(dt, p);
                            p = f.Write(d, p);
                            p = f.Write(i, p);
                            p = f.Write(g, p);

                            f.Write(ba, p, ba.Length);

                            p = 0;

                            bool     br  = false;
                            int      ir  = 0;
                            double   dr  = 0;
                            DateTime dtr = DateTime.MinValue;
                            char     cr  = char.MinValue;
                            Guid     gr  = Guid.Empty;
                            byte[]   bar = new byte[3];

                            p = f.Read(ref br, p);
                            p = f.Read(ref cr, p);
                            p = f.Read(ref dtr, p);
                            p = f.Read(ref dr, p);
                            p = f.Read(ref ir, p);
                            p = f.Read(ref gr, p);

                            f.Read(bar, p, 0);

                            if (br != b || cr != c || dtr != dt || dr != d || ir != i || gr != g || !Assert.SameValues(ba, bar))
                            {
                                Passed         = false;
                                FailureMessage = "The reads do not match the writes.";
                                return;
                            }
                        }

                        $"{hwName}: fragment reads and writes primitive types correctly.".AsSuccess();
                    }
                }
            }

            if (!Passed.HasValue)
            {
                Passed = true;
            }
            IsComplete = true;
        }
Exemplo n.º 12
0
        public async Task Start(IDictionary <string, List <string> > args)
        {
            await Task.Delay(0);

            if (args.ContainsKey("+all"))
            {
                args.Add("-store", new List <string>()
                {
                    "mh", "mmf", "nh"
                });
            }

            args.AssertAll("-store");
            var opt = args["-store"];

            opt.AssertNothingOutsideThese("mh", "mmf", "nh");

            const int ALLOC_SIZE  = 100;
            const int FRAGS_COUNT = 100;
            var       ccAllocs    = new int[3] {
                1, 2, 3
            };

            foreach (var cc in ccAllocs)
            {
                // The initial hw space will fit half of the fragments
                var stg = new HighwaySettings((FRAGS_COUNT / 2) * ALLOC_SIZE);
                var iH  = new Dictionary <string, IMemoryHighway>();

                // Update the no-luckGate capacity
                stg.ConcurrentNewLaneAllocations = cc;

                iH.Add("nh", new MarshalHighway(stg, stg.DefaultCapacity));
                iH.Add("mh", new HeapHighway(stg, stg.DefaultCapacity));
                iH.Add("mmf", new MappedHighway(stg, stg.DefaultCapacity));

                foreach (var kp in iH)
                {
                    if (opt.Contains(kp.Key))
                    {
                        var CCAllocs = new Task[FRAGS_COUNT];
                        var frags    = new MemoryFragment[FRAGS_COUNT];

                        using (var hw = kp.Value)
                        {
                            var hwName = hw.GetType().Name;

                            for (int i = 0; i < CCAllocs.Length; i++)
                            {
                                CCAllocs[i] = new Task((idx) => frags[(int)idx] = hw.AllocFragment(ALLOC_SIZE), i);
                            }

                            $"Starting all {CCAllocs.Length} concurrent allocations".AsInfo();

                            for (int i = 0; i < CCAllocs.Length; i++)
                            {
                                CCAllocs[i].Start();
                            }

                            Task.WaitAll(CCAllocs);

                            "Allocs complete".AsInfo();
                            var lanesCount = hw.GetLanesCount();
                            var totalFrags = hw.GetTotalActiveFragments();

                            if (lanesCount - 1 > cc)
                            {
                                Passed         = false;
                                FailureMessage = $"{hwName}: There are {lanesCount} lanes, cc: {cc}. Expected the no-luckGate to hold them off.";
                                return;
                            }

                            var nullFrags = frags.Where(x => x == null).Count();

                            if (nullFrags > 0)
                            {
                                Passed         = false;
                                FailureMessage = $"{hwName}: There are {nullFrags} null fragments. The ConcurrentNewLaneAllocations is {cc}.";
                                return;
                            }

                            $"{hwName}: Correctly allocated exactly {cc} new lanes.".AsSuccess();
                        }
                    }
                }
            }

            Passed     = true;
            IsComplete = true;
        }