Example #1
0
        private void SendMsg(byte[] msg)
        {
            if (m_mySocket == null)
            {
                return;
            }

            if (!m_isSocketReady)
            {
                return;
            }

            //lock (this)
            {
                if (m_mySocket.Client != null && m_mySocket.Client.Connected)
                {
                    try
                    {
                        m_mySocket.Client.Send(msg, 0, m_currentSendBufferWritePosition, SocketFlags.None);
                        m_currentSendBufferWritePosition = 0;
                    }
                    catch (Exception ex)
                    {
                        m_currentSendBufferWritePosition = 0;
                        DEBUG.Error("SendMsg Error: {0}", ex.Message.ToString());
                    }
                }
            }
        }
Example #2
0
File: PIM.cs Project: hoangt/PIMSim
        public PIM(ref InsPartition ins_p_)
        {
            if (Config.DEBUG_PIM)
            {
                DEBUG.WriteLine("PIM Module Initialed.");
            }
            ins_p = ins_p_;

            unit = new List <ComputationalUnit>();
            if (PIMConfigs.unit_type == PIM_Unit_Type.Processors)
            {
                if (Config.DEBUG_PIM)
                {
                    DEBUG.WriteLine("PIM Unit Type : Processors.");
                }
                for (int i = 0; i < PIMConfigs.N; i++)
                {
                    var p = new PIMProc(ref ins_p, i);
                    unit.Add(p);
                }
            }
            else
            {
                if (Config.DEBUG_PIM)
                {
                    DEBUG.WriteLine("PIM Unit Type : Pipeline.");
                }
                //pipeline mode
                // When PIMSim runs into pipeline mode, input should always be a Function.

                for (int i = 0; i < PIMConfigs.CU_Name.Count; i++)
                {
                    if (PIMConfigs.CU_Name[i] == "Customied")
                    {
                        //add your code here
                    }
                    else
                    {
                        if (PIMConfigs.CU_Name[i] == "Adder")
                        {
                            unit.Add(new Adder(i, ref ins_p) as ComputationalUnit);
                            return;
                        }
                        else
                        {
                            if (PIMConfigs.CU_Name[i] == "Adder_Conventional")
                            {
                                unit.Add(new Adder_Conventional(i, ref ins_p) as ComputationalUnit);
                                return;
                            }
                            else
                            {
                                DEBUG.Error("No PIM Unit templates.");
                                Environment.Exit(2);
                            }
                        }
                    }
                }
            }
        }
Example #3
0
        public void GetMsg(object o)
        {
            while (m_isSocketReady)
            {
                int recieveLen = 0;
                try
                {
                    if (m_mySocket.Available > 0)
                    {
                        recieveLen = m_mySocket.Client.Receive(m_getData, 0, (int)m_mySocket.ReceiveBufferSize, SocketFlags.None);
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(1);
                        break;
                    }
                }
                catch (Exception e)
                {
                    DEBUG.Error(e.Message);
                    break;
                }

                m_readBuffer.Put(m_getData, 0, recieveLen);
                Array.Clear(m_getData, 0, m_getData.Length);
                m_readBuffer.Flip();
                int len = GetPacketLength(m_readBuffer);

                if (len < 0)
                {
                    break;
                }

                MessageBody msg = new MessageBody();
                msg.size = (uint)CPacket.PopPacketLength(m_readBuffer);
                msg.type = (ushort)CPacket.PopPacketType(m_readBuffer);
                byte[] data = new byte[len - CPacket.HEADER_TYPE_BYTES];
                m_readBuffer.Get(data, 0, len - CPacket.HEADER_TYPE_BYTES);
                msg.data = data;

                m_recievedMsgs.Add(msg);

                bool isFinish = false;
                if (m_readBuffer.HasRemaining)
                {
                    m_readBuffer.Compact();
                }
                else
                {
                    m_readBuffer.Clear();
                    isFinish = true;
                }

                if (isFinish)
                {
                    break;
                }
            }
        }
Example #4
0
 public bool parse_args(string[] args)
 {
     if (args.Count() % 2 != 0 || args.Count() == 0)
     {
         DEBUG.Error("Please make sure that all the args are input correctly.");
         Environment.Exit(2);
     }
     for (int i = 0; i < args.Count(); i += 2)
     {
         string command = args[i].Replace("-", "");
         if (command.Equals("trace", StringComparison.OrdinalIgnoreCase) || command.Equals("t"))
         {
             Config.trace_path = args[i + 1];
         }
         else
         {
             if (command.Equals("config", StringComparison.OrdinalIgnoreCase))
             {
                 Config.config_path = args[i + 1];
             }
             else
             {
                 if (command.Equals("output", StringComparison.OrdinalIgnoreCase) || command.Equals("o"))
                 {
                     Config.output_file = args[i + 1];
                 }
                 else
                 {
                     if (command.Equals("n", StringComparison.OrdinalIgnoreCase))
                     {
                         Config.N = Int16.Parse(args[i + 1]);
                     }
                     else
                     {
                         if (command.Equals("c", StringComparison.OrdinalIgnoreCase) || command.Equals("cycle", StringComparison.OrdinalIgnoreCase))
                         {
                             Config.sim_type  = SIM_TYPE.cycle;
                             Config.sim_cycle = UInt64.Parse(args[i + 1]);
                         }
                         Usage();
                         Environment.Exit(1);
                     }
                 }
             }
         }
     }
     return(true);
 }
Example #5
0
        public void OnSight(SCSight ret)
        {
            if (ret == null)
            {
                DEBUG.Error("Invalid Proto");
                return;
            }

            DEBUG.Log("OnSight");

            for (int i = 0; i < ret.Players.Count; ++i)
            {
                PlayerInfo info = ret.Players[i];
                SetPlayerInfo(info);
                PlayerManager.Instance.AddOne(this);
            }
        }
Example #6
0
        public void ConnectSocket(string hostName, int port)
        {
            try
            {
                if (m_mySocket == null)
                {
                    m_mySocket = new TcpClient();
                }

                m_mySocket.BeginConnect(hostName, port, ConnectCallBack, null);
                m_isConnecting = true;
            }
            catch (Exception ex)
            {
                m_isConnecting = false;
                // TODO: 打印日志
                DEBUG.Error("连接服务器失败:{0}", ex.ToString());
            }
        }
Example #7
0
        void OnReady(params object[] args)
        {
            MessageBody body = args[0] as MessageBody;

            if (body == null)
            {
                return;
            }

            SCReady ret = ProtoSerialize.DeSerialize <SCReady>(body.data);

            if (ret == null)
            {
                DEBUG.Error("Invalid Proto");
                return;
            }

            DEBUG.Log("OnReady");
        }
Example #8
0
        public void OnLogin(SCLogin ret)
        {
            if (UUID != 0)
            {
                DEBUG.Error("Invalid HandeLogin UUID = {0}", UUID);
                return;
            }

            if (ret == null)
            {
                DEBUG.Error("Invalid Proto");
                return;
            }

            UUID = ret.UUID;

            PlayerManager.Instance.AddOne(this);

            DEBUG.Log("OnLogin:: UUID={0}", ret.UUID);
        }
Example #9
0
        void OnFrameTurn(params object[] args)
        {
            MessageBody body = args[0] as MessageBody;

            if (body == null)
            {
                return;
            }

            SCFrame proto = ProtoSerialize.DeSerialize <SCFrame>(body.data);

            if (proto == null)
            {
                DEBUG.Error("Invalid Proto");
                return;
            }

            FrameInfo frameInfo = new FrameInfo();

            frameInfo.CurFrameID = proto.FrameID;
            frameInfo.NxtFrameID = proto.NextFrameID;

            int size = proto.Users.Count;

            for (int i = 0; i < size; ++i)
            {
                UserFrame user = proto.Users[i];

                List <EPlayerEvent> frameInfoPool = new List <EPlayerEvent>();
                for (int j = 0; j < user.KeyInfo.Count; ++j)
                {
                    System.UInt32 key = user.KeyInfo[j];
                    frameInfoPool.Add((EPlayerEvent)key);
                }
                frameInfo.UUID2FrameInfoPool.Add(user.UUID, frameInfoPool);
            }

            FrameTurn(frameInfo);
            CurFrameID = frameInfo.NxtFrameID;
            // DEBUG.Log("OnFrameTurn:: CurFrameID={0}, Size={1}", CurFrameID, body.size);
        }
Example #10
0
        void OnFrameInit(params object[] args)
        {
            MessageBody body = args[0] as MessageBody;

            if (body == null)
            {
                return;
            }

            SCFrameInit proto = ProtoSerialize.DeSerialize <SCFrameInit>(body.data);

            if (proto == null)
            {
                DEBUG.Error("Invalid Proto");
                return;
            }

            CurFrameID = proto.CurFrameID;
            NxtFrameID = proto.NxtFrameID;
            DEBUG.Log("OnFrameInit:: CurFrameID={0} NxtFrameID={1}", CurFrameID, NxtFrameID);
        }
Example #11
0
 public static T DeSerialize <T>(byte[] msg)
 {
     try
     {
         using (MemoryStream ms = new MemoryStream())
         {
             //将消息写入流中
             ms.Write(msg, 0, msg.Length);
             //将流的位置归0
             ms.Position = 0;
             //使用工具反序列化对象
             T result = ProtoBuf.Serializer.Deserialize <T>(ms);
             return(result);
         }
     }
     catch (Exception ex)
     {
         DEBUG.Error("反序列化失败: " + ex.ToString());
         return(default(T));
     }
 }
Example #12
0
        public void Update()
        {
            if (m_mySocket == null)
            {
                return;
            }

            if (!m_isSocketReady)
            {
                return;
            }

            try
            {
                SendMsg();

                HandleRecieveMsg();
            }
            catch (Exception ex)
            {
                DEBUG.Error("处理消息失败:{0}", ex.ToString());
            }
        }
Example #13
0
        private void ConnectCallBack(IAsyncResult Ar)
        {
            try
            {
                m_isConnecting = false;

                //设置连接数据缓存池大小
                m_mySocket.ReceiveBufferSize = READ_SOCKET_BUFFER_SIZE;
                m_mySocket.SendBufferSize    = SEND_SOCKET_BUFFER_SIZE;
                //设置及时模式
                m_mySocket.NoDelay        = true;
                m_mySocket.Client.NoDelay = true;

                //结束异步连接请求
                m_mySocket.EndConnect(Ar);

                //uint IOC_IN = 0x80000000;
                //uint IOC_VENDOR = 0x18000000;
                //uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
                //m_mySocket.Client.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null);

                m_isSocketReady = true;

                m_getMsgThread = new Timer(GetMsg);
                m_getMsgThread.Change(TimeSpan.FromMilliseconds(0), TimeSpan.FromMilliseconds(1));

                //获得当前链接地址
                m_CurHost = GetConnectIp();
                //当前链接端口
                m_Port = GetConnectPort();
            }
            catch (Exception ex)
            {
                // TODO: 打印日志
                DEBUG.Error("客户端启动消息接收线程失败:{0}", ex.ToString());
            }
        }
Example #14
0
 public static byte[] Serialize <T>(T model)
 {
     try
     {
         //涉及格式转换,需要用到流,将二进制序列化到流中
         using (MemoryStream ms = new MemoryStream())
         {
             //使用ProtoBuf工具的序列化方法
             ProtoBuf.Serializer.Serialize <T>(ms, model);
             //定义二级制数组,保存序列化后的结果
             byte[] result = new byte[ms.Length];
             //将流的位置设为0,起始点
             ms.Position = 0;
             //将流中的内容读取到二进制数组中
             ms.Read(result, 0, result.Length);
             return(result);
         }
     }
     catch (Exception ex)
     {
         DEBUG.Error("序列化失败: " + ex.ToString());
         return(null);
     }
 }
Example #15
0
        public void CloseSocket()
        {
            if (m_getMsgThread != null)
            {
                m_getMsgThread.Dispose();
                m_getMsgThread = null;
            }

            if (m_mySocket != null)
            {
                try
                {
                    m_mySocket.Client.Shutdown(SocketShutdown.Both);
                }
                catch (Exception ex)
                {
                    // TODO: 打印日志
                    DEBUG.Error("断开连接失败:{0}", ex.ToString());
                }
                m_mySocket.Close();
            }
            m_mySocket      = null;
            m_isSocketReady = false;
        }
Example #16
0
        /// <summary>
        /// Get instruction from instruction partitioner.
        /// We assume that host core can only process intructions(Not Functions and InstrcutionBlocks).
        /// When core encountered a non-instruction, return error.
        /// </summary>
        /// <returns>Instruction to be processed. If none, NOP.</returns>
        public Instruction get_ins_from_insp()
        {
            Input tp;

            if (Config.trace_type == Trace_Type.PC)
            {
                tp = ins_p.get_req(this.pid, true, this.pc);
            }
            else
            {
                tp = ins_p.get_req(this.pid, true);
            }

            //indentify if input is an instruction
            if (tp is Instruction)
            {
                switch ((tp as Instruction).type)
                {
                case InstructionType.READ:
                    read_reqs++;
                    break;

                case InstructionType.WRITE:
                    write_reqs++;
                    break;

                case InstructionType.NOP:
                    nop++;
                    break;

                case InstructionType.CALCULATION:
                    cal_ins++;
                    break;

                default:
                    break;
                }
                //return instruction
                if ((tp as Instruction).is_mem)
                {
                    (tp as Instruction).block_addr = tlb.scan_page((tp as Instruction).address);
                }

                if (Config.DEBUG_PROC)
                {
                    DEBUG.WriteLine("-- Current Instruction : " + (tp as Instruction).ToString());
                }
                return(tp as Instruction);
            }
            else
            {
                if (tp is PCTrace)
                {
                    pc = (tp as PCTrace).PC - 1;
                    var res = (tp as PCTrace).parsetoIns();
                    res.block_addr = tlb.scan_page(res.address);
                    return(res);
                }
                else
                {
                    //Host processor can only process Instructions
                    if (Config.DEBUG_PROC)
                    {
                        DEBUG.Error("-- Receieved a none Instrcution Input.");
                    }
                    Environment.Exit(Error.InputArgsError);
                    return(null);
                }
            }
        }
Example #17
0
        public Instruction parse_ins(string line, int pid_)
        {
            try
            {
                //try one line
                DEBUG.Assert(line != null);

                string[]    split_ins = line.Split('|');
                Instruction ins       = new Instruction(split_ins[1]);
                ins.cycle = UInt64.Parse(split_ins[0]);
                ins.pid   = pid_;

                if (PIMConfigs.PIM_Fliter == PIM_input_type.All)
                {
                    //set all insruction to pim
                    ins.pim = PIMConfigs.PIM_Ins_List.Any(s => s == ins.Operation) ? true : false;
                }
                else
                {
                    //use PIM_ label to identify PIM operations
                    ins.pim = (ins.Operation.StartsWith("PIM_")) ? true : false;
                }
                //read address and data.
                if (split_ins.Length >= 4)
                {
                    ins.type = (split_ins[2] == "R") ? InstructionType.READ : InstructionType.WRITE;
                    //  ins.type = (split_ins[2] == "W") ? InstructionType.WRITE : InstructionType.CALCULATION;



                    string[] d_and_a = split_ins[3].Split(' ');
                    ins.is_mem = false;

                    //read data and address
                    foreach (string p in d_and_a)
                    {
                        if (p.Contains("A="))
                        {
                            ins.address = UInt64.Parse(p.Replace("A=0x", ""), System.Globalization.NumberStyles.AllowHexSpecifier);
                            ins.is_mem  = true;
                        }
                        else
                        {
                            if (p.Contains("D="))
                            {
                                ins.data   = UInt64.Parse(p.Replace("D=0x", ""), System.Globalization.NumberStyles.AllowHexSpecifier);
                                ins.is_mem = true;
                            }
                            else
                            {
                                DEBUG.Error("Error in parsing trace line.");
                                Environment.Exit(2);
                            }
                        }
                    }
                }
                else
                {
                    ins.type = InstructionType.CALCULATION;
                }
                return(ins);
            }

            catch
            {
                DEBUG.Error("Faied to parse trace in CPU:" + pid_ + "line=" + line);
                return(null);
            }
        }
Example #18
0
        public PIMSimulator(string[] args)
        {
            initAllconfigs(args);
            trace = new TraceFetcher();


            ins_p = new InsPartition();

            pg = new PageConverter();

            if (Config.shared_cache)
            {
                shared_cache = new Shared_Cache();
            }
            proc = new List <Proc>();

            for (int i = 0; i < Config.N; i++)
            {
                Proc to_add = new Proc(ref ins_p, i);
                if (Config.shared_cache)
                {
                    to_add.attach_shared_cache(ref shared_cache);
                }
                to_add.attach_tlb(ref pg);
                proc.Add(to_add);
            }
            int count = 0;

            foreach (var item in Config.memory)
            {
                if (item.Key.Equals("HMC"))
                {
                    var tp = new HMCMem(count++) as MemObject;
                    MemorySelector.add(item.Value, ref tp);
                }
                else
                {
                    if (item.Key.Equals("DRAM") || item.Key.Equals("PCM"))
                    {
                        var tp = new DDRMem(count++) as MemObject;
                        MemorySelector.add(item.Value, ref tp);
                    }
                    else
                    {
                        //error
                        DEBUG.Error("Unknown Memory Type.");
                        Environment.Exit(3);
                    }
                }
            }

            Mctrl.init_queue();

            PIMMctrl.init_queue();


            pim = new PIM.PIM(ref ins_p);
            Coherence.init();
            Coherence.linkproc(proc);
            GlobalTimer.InitClock();
            BuildTopology();
        }
Example #19
0
 /// <summary>
 /// read config file
 /// </summary>
 public static void read_configs()
 {
     try
     {
         FileStream   fs   = new FileStream(config_file, FileMode.Open);
         StreamReader sr   = new StreamReader(fs);
         string       line = "";
         while ((line = sr.ReadLine()) != null)
         {
             if (line.Contains(";"))
             {
                 line = line.Substring(0, line.IndexOf(";"));
             }
             if (line.StartsWith("#"))
             {
                 continue;
             }
             if (line.Contains("="))
             {
                 string[] split = line.Replace(" ", "").Split('=');
                 if (line.Count() % 2 != 0)
                 {
                     DEBUG.WriteLine("Please make sure the line is correct: " + line);
                     continue;
                 }
                 if (split[0] == "RAM")
                 {
                     string[] ram = split[1].Replace("[", "").Replace("]", "").Split(',');
                     if (ram.Count() % 2 != 0)
                     {
                         DEBUG.WriteLine("Please make sure the line is correct: " + line);
                         Environment.Exit(2);
                     }
                     for (int i = 0; i < ram.Count(); i += 2)
                     {
                         memory.Add(new KeyValuePair <string, int>(ram[i + 1], Int16.Parse(ram[i])));
                     }
                     if (memory.Count <= 0)
                     {
                         DEBUG.Error("No Memory?");
                         Environment.Exit(2);
                     }
                     else
                     {
                         if (memory.Count == 1)
                         {
                             if (memory[0].Key == "HMC")
                             {
                                 Config.ram_type = RAM_TYPE.HMC;
                             }
                             else
                             {
                                 if (memory[0].Key == "DRAM")
                                 {
                                     Config.ram_type = RAM_TYPE.DRAM;
                                 }
                                 else
                                 {
                                     if (memory[0].Key == "PCM")
                                     {
                                         Config.ram_type = RAM_TYPE.PCM;
                                     }
                                     else
                                     {
                                         DEBUG.Error("Error in input Memory Type.");
                                         Environment.Exit(2);
                                     }
                                 }
                             }
                         }
                         else
                         {
                             Config.ram_type = RAM_TYPE.HYBRID;
                         }
                     }
                     continue;
                 }
                 SetValue(split[0], split[1]);
             }
         }
     }
     catch
     {
         DEBUG.WriteLine("ERROR : cannot read configs. ");
     }
 }
Example #20
0
        /// <summary>
        /// Get inputs from instruction partitioner.
        /// We assume that host core can only process intructions(Not Functions and InstrcutionBlocks).
        /// When core encountered a non-instruction, return error.
        /// </summary>
        /// <returns>Instruction to be processed. If none, NOP.</returns>
        public Instruction get_ins_from_insp()
        {
            //current block has un-processed instructions
            if (current_block != null)
            {
                //get current instructions
                var item = current_block.get_ins(this.cycle);
                if (item != null)
                {
                    bandwidth_bit = bandwidth_bit + item.Length();
                    return(item);
                }
                else
                {
                    //current block is empty
                    if (block_latency.Any(s => s.Key == current_block.name))
                    {
                        block_latency[current_block.name] += cycle - current_block.servetime;
                    }
                    block_latency.Add(current_block.name, cycle - current_block.servetime);
                    current_block = null;
                }
            }

            Input tp;

            if (Config.trace_type == Trace_Type.PC)
            {
                tp = ins_p.get_req(this.pid, false, this.pc);
            }
            else
            {
                tp = ins_p.get_req(this.pid, false);
            }
            if (tp is Instruction)
            {
                switch ((tp as Instruction).type)
                {
                case InstructionType.READ:
                    read_reqs++;
                    break;

                case InstructionType.WRITE:
                    write_reqs++;
                    break;

                case InstructionType.NOP:
                    nop++;
                    break;

                case InstructionType.CALCULATION:
                    cal_ins++;
                    break;

                default:
                    break;
                }

                if ((tp as Instruction).is_mem)
                {
                    (tp as Instruction).block_addr = tlb.scan_page((tp as Instruction).address);
                }

                if (Config.DEBUG_PIM)
                {
                    DEBUG.WriteLine("-- Current Instruction : " + (tp as Instruction).ToString());
                }
                bandwidth_bit = bandwidth_bit + (tp as Instruction).Length();
                return(tp as Instruction);
            }
            else
            {
                if (tp is InstructionBlock)
                {
                    total_block_load++;
                    current_block = tp as InstructionBlock;
                    var item = current_block.get_ins(this.cycle);
                    if (item == null)
                    {
                        DEBUG.Error("Block Instructions cannot be none!");
                        Environment.Exit(2);
                    }
                    if (block_count.Any(s => s.Key == current_block.name))
                    {
                        block_count[current_block.name]++;
                    }
                    else
                    {
                        block_count.Add(current_block.name, 1);
                    }
                    if (Config.DEBUG_PIM)
                    {
                        DEBUG.WriteLine("-- Fetched Block : " + (tp as InstructionBlock).ToString());
                    }
                    bandwidth_bit = bandwidth_bit + (item as Instruction).Length();
                    return(item);
                }
                else
                {
                    if (tp is PCTrace)
                    {
                        pc = (tp as PCTrace).PC - 1;
                        var res = (tp as PCTrace).parsetoIns();
                        res.block_addr = tlb.scan_page(res.address);
                        return(res);
                    }
                    else
                    {
                        if (Config.DEBUG_PIM)
                        {
                            DEBUG.Error("-- Receieved a FUNCTION Input.");
                        }
                        Environment.Exit(Error.InputArgsError);
                        return(null);
                    }
                }
            }
        }
Example #21
0
 public virtual Protocol Clone()
 {
     DEBUG.Error("This Function is Deprecated!");
     return(null);
 }