/* CreateNode() Crea un nuovo nodo e lo inserisce nella lista (prima di creare un nuovo nodo controlla se ne esistono di disponibili). Deve ricevere il puntatore ai dati da associare al nodo. */ CNode* CNodeList::CreateNode(void* ptr) { CNode* pNode; // controlla se esistono nodi inutilizzati if((pNode = FindFirstNode(UNUSED_NODE))!=(CNode*)NULL) { // riusa il nodo unitilizzato associandogli i dati InitializeNode(pNode,USED_NODE,ptr); } else { // nessun nodo disponibile, ne crea uno nuovo pNode = (CNode*)new CNode(); if(pNode!=(CNode*)NULL) { // inserisce un nuovo nodo associandogli i dati InitializeNode(pNode,UNUSED_NODE,ptr); InsertNode(pNode); } } // indice del nodo/totale dei nodi presenti if(pNode) pNode->index = m_nTot++; // rinumera i nodi occupati EnumerateNodes(); return(pNode); }
/* ReleaseNode() Rilascia il nodo. Notare che i nodi non vengono mai rilasciati fisicamente, ma riciclati. Il rilascio consiste nel rilasciarne le risorse, reinizializzarlo e marcarlo come inutilizzato. */ BOOL CNodeList::ReleaseNode(CNode* pNode,int nMode) { BOOL flag = FALSE; // controlla il puntatore if(pNode!=(CNode*)NULL) { // controlla lo status if(pNode->status==USED_NODE || pNode->status==REUSED_NODE) { flag = TRUE; // per permettere all'eventuale classe derivata di rilasciare le risorse associate al // nodo dei dati (ossia le risorse contenute in pNode->data) // notare che se i dati presenti in pNode->data sono o contengono una classe non e' sufficente // la delete di cui sotto, per cui, nella ridefinizione della virtuale, oltre ad eliminare // i dati contenuti in pNode->data bisogna eliminare anche pNode->data, effettuando i cast // opportuni affinche venga chiamato il distruttore adeguato if(nMode==RELEASE_DELETE_MODE || nMode==RELEASE_ERASE_MODE) { if(!PreDelete(pNode)) { // elimina il nodo di dati if(pNode->data) delete pNode->data; } // deve rimettere a NULL in modo tale che se il nodo viene riutilizzato, la chiamata alla // virtuale Initialize() generi automaticamente la creazione dei dati con la chiamata alla // virtuale Create() pNode->data = NULL; // marca il nodo come inutilizzato if(nMode==RELEASE_DELETE_MODE) InitializeNode(pNode,UNUSED_NODE,(void*)NULL); // marca il nodo come cancellato else if(nMode==RELEASE_ERASE_MODE) InitializeNode(pNode,ERASED_NODE,(void*)NULL); } else if(nMode==RELEASE_REMOVE_MODE) { // marca il nodo come inutilizzato InitializeNode(pNode,REUSED_NODE,Initialize(pNode->data)); } m_nTot--; EnumerateNodes(); } } return(flag); }
void CChessBorad::OnTimer(UINT nIDEvent) { if (m_IsConnect) { TCP_PACKAGE tcpPackage; tcpPackage.cmdType = CT_NETTEST; m_ClientSock.Send(&tcpPackage,sizeof(TCP_PACKAGE)); //发送网络测试信息 m_TestNum++; if (m_TestNum > 3) //对方掉线,游戏结束 { m_TestNum = 0; m_IsDown = FALSE; m_IsStart = FALSE; m_IsWin = FALSE; m_State = esEND; m_IsConnect = FALSE; InitializeNode(); CSrvFiveChessDlg * pDlg = (CSrvFiveChessDlg*)GetParent(); pDlg->m_RightPanel.m_NetState.SetWindowText("网路状态:断开连接"); //更新界面 Invalidate(); m_LocalChessPT.x = m_LocalChessPT.y = -1; m_RemoteChessPT.x = m_RemoteChessPT.y = -1; } } CDialog::OnTimer(nIDEvent); }
BOOL CChessBorad::OnInitDialog() { CDialog::OnInitDialog(); int nOriginX = m_nOrginX*m_fRateX; int nOriginY = m_nOrginY*m_fRateY; int nCellWidth = m_nCellWidth*m_fRateX; int nCellHeight = m_nCellHeight*m_fRateY; //创建套接字 m_ClientSock.AttachDlg(this); m_SrvSock.AttachDlg(this); m_ClientSock.Create(); InitializeNode(); //设置棋子坐标 for (int i=0; i<m_nRowCount+1; i++) { for (int j=0; j<m_nColCount+1; j++) { m_NodeList[i][j].m_Point= CPoint(nOriginX+nCellWidth*j,nOriginY+nCellHeight*i); } } for (int m=0; m<m_nRowCount+1; m++) { for (int n=0; n<m_nColCount+1; n++) { SetRecentNode(&m_NodeList[m][n]); } } return TRUE; }
void CChessBorad::Start() { if (m_IsStart == FALSE) { m_IsStart = TRUE; InitializeNode(); } }
/* * ======== main ======== */ int main(int argc, char **argv) { INT nStrmMode = 0; FILE *inFile = NULL; /* Input file handle. */ FILE *outFile = NULL; /* Output file handle. */ struct STRMCOPY_TASK strmcopyTask; int status = 0; DspManager_Open(argc, NULL); /* Process command line arguments, open data files: */ status = ProcessArgs(argc, argv, &inFile, &outFile, &nStrmMode); strmcopyTask.hProcessor = NULL; strmcopyTask.hNode = NULL; strmcopyTask.hInStream = NULL; strmcopyTask.hOutStream = NULL; strmcopyTask.ppInBufs = NULL; strmcopyTask.ppOutBufs = NULL; strmcopyTask.nStrmMode = nStrmMode; if (DSP_SUCCEEDED(status)) { /* Perform processor level initialization. */ status = InitializeProcessor(&strmcopyTask); } if (DSP_SUCCEEDED(status)) { /* Perform node level initialization. */ status = InitializeNode(&strmcopyTask); if (DSP_SUCCEEDED(status)) { /* Perform stream level initialization. */ status = InitializeStreams(&strmcopyTask); if (DSP_SUCCEEDED(status)) { /* Run task. */ status = RunTask(&strmcopyTask, inFile, outFile); if (DSP_SUCCEEDED(status)) { fprintf(stdout, "RunTask succeeded.\n"); } else { fprintf(stdout, "RunTask failed.\n"); } } } } /* Close opened files. */ if (inFile) { fclose(inFile); } if (outFile) { fclose(outFile); } CleanupStreams(&strmcopyTask); CleanupNode(&strmcopyTask); CleanupProcessor(&strmcopyTask); DspManager_Close(0, NULL); /*printf("Hit enter to exit: "); (void)getchar(); */ return (DSP_SUCCEEDED(status) ? 0 : -1); }
/* * ======== main ======== */ int main(int argc, char **argv) { struct SCALE_TASK scaleTask; int status = 0; status = (DBAPI)DspManager_Open(0, NULL); if (DSP_FAILED(status)) { printf("\nFATAL ERROR: Bridge Initialisation FAILED\n"); return 0; } status = ProcessArgs(argc, argv, &scaleTask); /* initialize context object */ scaleTask.hProcessor = NULL; scaleTask.hNode = NULL; scaleTask.hInStream = NULL; scaleTask.hOutStream = NULL; scaleTask.pInBuf = NULL; scaleTask.pOutBuf = NULL; if (DSP_SUCCEEDED(status)) { /* do processor level initialization */ status = InitializeProcessor(&scaleTask); if (DSP_SUCCEEDED(status)) { /* do node level initialization */ status = InitializeNode(&scaleTask); if (DSP_SUCCEEDED(status)) { /* do stream level initialization */ status = InitializeStreams(&scaleTask); if (DSP_SUCCEEDED(status)) { /* do processing */ status = RunTask(&scaleTask); if (DSP_SUCCEEDED(status)) { fprintf(stdout, "\nRunTask succeeded.\n"); } else { fprintf(stdout, "\nRunTask failed.\n"); } } } } } CleanupStreams(&scaleTask); CleanupNode(&scaleTask); CleanupProcessor(&scaleTask); status = DspManager_Close(0, NULL); if (DSP_FAILED(status)) { printf("\nERROR: Bridge Close FAILED\n"); } return (DSP_SUCCEEDED(status) ? 0 : -1); }
void NetGraph::Initialize() { // check for nodes with multiple backprop connections bool no_multiple_connections = true; do { no_multiple_connections = true; std::vector<NetGraphNode*> nodes(nodes_); for (NetGraphNode* node : nodes) { if(node->backprop_connections.size() > 1 && dynamic_cast<GradientAccumulationLayer*>(node->layer) == NULL) { no_multiple_connections = false; LOGINFO << "Node has multiple backprop connections: " << node->layer->GetLayerDescription(); // Insert gradient accumulation layer GradientAccumulationLayer* ga = new GradientAccumulationLayer(node->backprop_connections.size()); NetGraphNode* ga_node = new NetGraphNode(ga, NetGraphConnection(node)); AddNode(ga_node); // Redirect input connections using backprop connections int b = 0; for(NetGraphBackpropConnection& backprop_connection : node->backprop_connections) { if(backprop_connection.node != ga_node) { for(NetGraphConnection& target_connection : backprop_connection.node->input_connections) { if(target_connection.node == node && target_connection.buffer == backprop_connection.buffer && target_connection.backprop) { target_connection.node = ga_node; backprop_connection.buffer = b; target_connection.buffer = b++; } } ga_node->backprop_connections.push_back(backprop_connection); } } // Remove backprop connections from node auto predicate = [&](NetGraphBackpropConnection backprop_connection){ return backprop_connection.node != ga_node; }; node->backprop_connections.erase(std::remove_if(node->backprop_connections.begin(), node->backprop_connections.end(), predicate), node->backprop_connections.end()); } } } while(!no_multiple_connections); for (NetGraphNode* node : nodes_){ InitializeNode(node); } }
void NetGraph::InitializeNode(NetGraphNode* node) { if (!node->initialized) { // Collect input tensors through DFS std::vector<CombinedTensor*> input_tensors; for (NetGraphConnection connection : node->input_connections) { InitializeNode(connection.node); input_tensors.push_back(connection.node->output_buffers[connection.buffer].combined_tensor); } // Ask layer to create output buffers std::vector<CombinedTensor*> output_tensors; bool success_outputs = node->layer->CreateOutputs(input_tensors, output_tensors); // Verify output buffer creation if (!success_outputs) { FATAL("Layer will not create outputs: " << node->layer->GetLayerDescription() << ", input0: " << input_tensors[0]->data); } // Verify output buffer count if (output_tensors.size() != node->output_buffers.size()) FATAL("Node created wrong number of output buffers!"); // Update node output buffer info for (unsigned int b = 0; b < output_tensors.size(); b++) { node->output_buffers[b].combined_tensor = output_tensors[b]; } // Connect layer bool success_connect = node->layer->Connect(input_tensors, output_tensors, this); if (!success_connect) FATAL("Layer will not connect: " << node->layer->GetLayerDescription()); // Save to flag node->initialized = true; } }
void CChessBorad::ReceiveData() { BYTE* pBuffer = new BYTE[sizeof(TCP_PACKAGE)]; int factlen = m_ClientSock.Receive(pBuffer,sizeof(TCP_PACKAGE)); if (factlen == sizeof(TCP_PACKAGE)) { TCP_PACKAGE tcpPackage; memcpy(&tcpPackage,pBuffer,sizeof(TCP_PACKAGE)); if (tcpPackage.cmdType == CT_NETTEST )//测试网络状态 { m_TestNum = 0; CSrvFiveChessDlg * pDlg = (CSrvFiveChessDlg*)GetParent(); pDlg->m_RightPanel.m_NetState.SetWindowText("网路状态:已连接"); m_IsConnect = TRUE; } else if (tcpPackage.cmdType == CT_BEGINGAME)//开始游戏 { CSrvFiveChessDlg *pDlg = (CSrvFiveChessDlg*)GetParent(); //设置用户昵称 pDlg->m_RightPanel.m_UserList.SetItemText(1,0,tcpPackage.chText); Start(); //获取服务器昵称 CString csNickName; csNickName = pDlg->m_RightPanel.m_UserList.GetItemText(0,0); if (csNickName.IsEmpty()) csNickName = "匿名"; strncpy(tcpPackage.chText,csNickName,512); m_ClientSock.Send(&tcpPackage,sizeof(TCP_PACKAGE)); m_TestNum = 0; KillTimer(1); //开始网络测试 SetTimer(1,2000,NULL); m_State = esBEGIN; m_LocalChessPT.x = m_LocalChessPT.y = -1; m_RemoteChessPT.x = m_RemoteChessPT.y = -1; FreeBackPlayList(); m_bBackPlay = FALSE; } else if (tcpPackage.cmdType == CT_POINT)//客户端棋子坐标信息 { int nX = tcpPackage.chessPT.x; int nY = tcpPackage.chessPT.y; m_NodeList[nX][nY].m_Color = ncWHITE; NODE *pNode = new NODE(); memcpy(pNode,&m_NodeList[nX][nY],sizeof(NODE)); m_BackPlayList.AddTail(pNode); m_RemoteChessPT.x = nX; m_RemoteChessPT.y = nY; OnPaint(); m_IsDown = TRUE; } else if (tcpPackage.cmdType == CT_WINPOINT)//客户端赢了,客户端棋子起始和终止坐标信息 { int nStartX = tcpPackage.winPT[0].x; int nStartY = tcpPackage.winPT[0].y; int nEndX = tcpPackage.winPT[1].x; int nEndY = tcpPackage.winPT[1].y; m_Startpt = tcpPackage.winPT[0]; m_Endpt = tcpPackage.winPT[1]; m_IsDown = FALSE; m_IsStart = FALSE; m_IsWin = TRUE; m_State = esEND; Invalidate(); MessageBox("你输了!!!"); m_IsWin = FALSE; InitializeNode(); Invalidate(); m_LocalChessPT.x = m_LocalChessPT.y = -1; m_RemoteChessPT.x = m_RemoteChessPT.y = -1; } else if (tcpPackage.cmdType == CT_TEXT) //文本信息 { CSrvFiveChessDlg *pDlg = (CSrvFiveChessDlg*)GetParent(); //获取对方昵称 CString csNickName = pDlg->m_RightPanel.m_UserList.GetItemText(1,0); CString csText = csNickName; csText += "说:"; csText += tcpPackage.chText; pDlg->m_RightPanel.m_MsgList.SetSel(-1,-1); pDlg->m_RightPanel.m_MsgList.ReplaceSel(csText); pDlg->m_RightPanel.m_MsgList.SetSel(-1,-1); pDlg->m_RightPanel.m_MsgList.ReplaceSel("\n"); } else if (tcpPackage.cmdType == CT_BACKDENY) //对方拒绝悔棋 { MessageBox("对方拒绝悔棋!","提示"); } else if (tcpPackage.cmdType == CT_BACKACCEPTANCE)//对方同意悔棋 { CSrvFiveChessDlg *pDlg = (CSrvFiveChessDlg*)GetParent(); //判断是否该本地用户下棋了,如果是则需要撤销之前对方下的棋子,然后再撤销本地用户下的棋子 if (pDlg->m_ChessBoard.m_IsDown==TRUE) { int nPosX = m_RemoteChessPT.x; int nPosY = m_RemoteChessPT.y; if (nPosX > -1 && nPosY > -1) { m_NodeList[nPosX][nPosY].m_Color = ncUNKOWN; NODE *pNode = new NODE(); memcpy(pNode,&m_NodeList[nPosX][nPosY],sizeof(NODE)); m_BackPlayList.AddTail(pNode); } nPosX = m_LocalChessPT.x; nPosY = m_LocalChessPT.y; if (nPosX > -1 && nPosY > -1) { m_NodeList[nPosX][nPosY].m_Color = ncUNKOWN; NODE *pNode = new NODE(); memcpy(pNode,&m_NodeList[nPosX][nPosY],sizeof(NODE)); m_BackPlayList.AddTail(pNode); } //刷新窗口 Invalidate(); } else //该对方下棋了,只撤销本地用户下的棋子 { int nPosX = m_LocalChessPT.x; int nPosY = m_LocalChessPT.y; if (nPosX > -1 && nPosY > -1) { m_NodeList[nPosX][nPosY].m_Color = ncUNKOWN; NODE *pNode = new NODE(); memcpy(pNode,&m_NodeList[nPosX][nPosY],sizeof(NODE)); m_BackPlayList.AddTail(pNode); //刷新窗口 Invalidate(); m_IsDown = TRUE; } } m_LocalChessPT.x = -1; m_LocalChessPT.y = -1; m_RemoteChessPT.x = -1; m_RemoteChessPT.y = -1; } else if (tcpPackage.cmdType == CT_BACKREQUEST) //对方发送悔棋请求 { if (MessageBox("是否同意悔棋?","提示",MB_YESNO)==IDYES) { CSrvFiveChessDlg *pDlg = (CSrvFiveChessDlg*)GetParent(); //接收悔棋 tcpPackage.cmdType = CT_BACKACCEPTANCE; m_ClientSock.Send(&tcpPackage,sizeof(TCP_PACKAGE)); //进行本地的悔棋处理 if (m_IsDown==TRUE) //该本地下棋了,只需要撤销一步 { int nPosX = m_RemoteChessPT.x; int nPosY = m_RemoteChessPT.y; if (nPosX > -1 && nPosY > -1) { m_NodeList[nPosX][nPosY].m_Color = ncUNKOWN; NODE *pNode = new NODE(); memcpy(pNode,&m_NodeList[nPosX][nPosY],sizeof(NODE)); m_BackPlayList.AddTail(pNode); //刷新窗口 Invalidate(); } m_IsDown = FALSE; } else //该对方下棋了,需要撤销两步 { int nPosX = m_LocalChessPT.x; int nPosY = m_LocalChessPT.y; if (nPosX > -1 && nPosY > -1) { m_NodeList[nPosX][nPosY].m_Color = ncUNKOWN; NODE *pNode = new NODE(); memcpy(pNode,&m_NodeList[nPosX][nPosY],sizeof(NODE)); m_BackPlayList.AddTail(pNode); } nPosX = m_RemoteChessPT.x; nPosY = m_RemoteChessPT.y; if (nPosX > -1 && nPosY > -1) { m_NodeList[nPosX][nPosY].m_Color = ncUNKOWN; NODE *pNode = new NODE(); memcpy(pNode,&m_NodeList[nPosX][nPosY],sizeof(NODE)); m_BackPlayList.AddTail(pNode); } //刷新窗口 Invalidate(); } m_LocalChessPT.x = -1; m_LocalChessPT.y = -1; m_RemoteChessPT.x = -1; m_RemoteChessPT.y = -1; } else //拒绝悔棋 { tcpPackage.cmdType = CT_BACKDENY; m_ClientSock.Send(&tcpPackage,sizeof(TCP_PACKAGE)); } } else if (tcpPackage.cmdType == CT_DRAWCHESSACCEPTANCE) //对方接收和棋请求 { CSrvFiveChessDlg *pDlg = (CSrvFiveChessDlg*)GetParent(); //进行和棋处理,游戏结束 m_TestNum = 0; m_IsDown = FALSE; m_IsStart = FALSE; m_IsWin = FALSE; m_State = esEND; InitializeNode(); //更新界面 Invalidate(); m_LocalChessPT.x = m_LocalChessPT.y = -1; m_RemoteChessPT.x = m_RemoteChessPT.y = -1; MessageBox("对方同意和棋!","提示"); } else if (tcpPackage.cmdType == CT_DRAWCHESSDENY) //对方拒绝和棋 { MessageBox("对方拒绝了和棋!","提示"); } else if (tcpPackage.cmdType == CT_DRAWCHESSREQUEST) //对方发出和棋请求 { CSrvFiveChessDlg *pDlg = (CSrvFiveChessDlg*)GetParent(); if (MessageBox("对方要求和棋,是否同意和棋?","提示",MB_YESNO)==IDYES) { //同意和棋 tcpPackage.cmdType = CT_DRAWCHESSACCEPTANCE; m_ClientSock.Send(&tcpPackage,sizeof(TCP_PACKAGE)); //进行和棋处理,游戏结束 m_TestNum = 0; m_IsDown = FALSE; m_IsStart = FALSE; m_IsWin = FALSE; m_State = esEND; InitializeNode(); //更新界面 Invalidate(); m_LocalChessPT.x = m_LocalChessPT.y = -1; m_RemoteChessPT.x = m_RemoteChessPT.y = -1; } else //拒绝和棋 { tcpPackage.cmdType = CT_DRAWCHESSDENY; m_ClientSock.Send(&tcpPackage,sizeof(TCP_PACKAGE)); } } else if (tcpPackage.cmdType == CT_GIVEUP) //对方认输了 { CSrvFiveChessDlg *pDlg = (CSrvFiveChessDlg*)GetParent(); //结束游戏 m_TestNum = 0; m_IsDown = FALSE; m_IsStart = FALSE; m_IsWin = FALSE; m_State = esEND; InitializeNode(); //更新界面 Invalidate(); m_LocalChessPT.x = m_LocalChessPT.y = -1; m_RemoteChessPT.x = m_RemoteChessPT.y = -1; MessageBox("您获胜了!","提示"); } } delete []pBuffer; }
void CChessBorad::OnLButtonUp(UINT nFlags, CPoint point) { CPoint pt = point; if (m_IsStart == FALSE) //游戏终止 return; if (m_IsDown==TRUE) //轮到客户端 { NODE* node = GetLikeNode(pt); if (node !=NULL) { if (node->m_Color==ncUNKOWN) { node->m_Color = ncBLACK; NODE *pNode = new NODE(); memcpy(pNode,node,sizeof(NODE)); m_BackPlayList.AddTail(pNode); OnPaint(); //定义一个TCP数据报 TCP_PACKAGE package; package.cmdType = CT_POINT; package.chessPT.x = node->m_nX; package.chessPT.y = node->m_nY; m_ClientSock.Send((void*)&package,sizeof(TCP_PACKAGE)); //记录最近的棋子坐标 m_LocalChessPT.x = node->m_nX; m_LocalChessPT.y = node->m_nY; m_IsDown= FALSE; if (IsWin(node)!= NULL) { m_IsStart = FALSE; Sleep(1000); //赢棋 TCP_PACKAGE winPackage; winPackage.cmdType = CT_WINPOINT; winPackage.winPT[0] = m_Startpt; winPackage.winPT[1] = m_Endpt; m_ClientSock.Send((void*)&winPackage,sizeof(TCP_PACKAGE)); m_IsWin = TRUE; m_State = esEND; Invalidate(); MessageBox("恭喜你,赢了!!!"); m_IsWin = FALSE; InitializeNode(); Invalidate(); m_LocalChessPT.x = m_LocalChessPT.y = -1; m_RemoteChessPT.x = m_RemoteChessPT.y = -1; } } } } CDialog::OnLButtonUp(nFlags, point); }