int main(int argc, char *argv[]) { int argPos, argNum = argc, nThreads = 1; char *netName = (char *) NULL; size_t timeStepNum, timeStep, timeHour; NCRnetwork_t *network; void *runoff, *outflow; int dt = 6; int cellId; for (argPos = 1; argPos < argNum;) { if (CMargTest (argv[argPos], "-n", "--network")) { if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) { CMmsgPrint(CMmsgUsrError, "%s: Missing sampling coverage!\n", CMfileName(argv[0])); return (-1); } netName = argv[argPos]; argNum = CMargShiftLeft(argPos, argv, argNum); continue; } if (CMargTest (argv[argPos], "-p", "--threads")) { if ((argNum = CMargShiftLeft(argPos, argv, argNum)) <= argPos) { CMmsgPrint(CMmsgUsrError, "%s: Missing sampling coverage!\n", CMfileName(argv[0])); return (-1); } if (sscanf(argv[argPos], "%d", &nThreads) != 1) { CMmsgPrint(CMmsgUsrError, "%s: Invalid threads [%s]\n", CMfileName(argv[0]), argv[argPos]); return (-1); } argNum = CMargShiftLeft(argPos, argv, argNum); continue; } if (CMargTest (argv[argPos], "-h", "--help")) { CMmsgPrint(CMmsgInfo, "%s [options] <nc grid> <nc grid>\n", CMfileName(argv[0])); CMmsgPrint(CMmsgInfo, " -n,--network\n"); CMmsgPrint(CMmsgInfo, " -p,--threads\n"); CMmsgPrint(CMmsgInfo, " -h,--help\n"); return (0); } if ((argv[argPos][0] == '-') && (strlen(argv[argPos]) > 1)) { fprintf(stderr, "Unknown option: %s!\n", argv[argPos]); return (-1); } argPos++; } if (argNum != 3) { CMmsgPrint(CMmsgInfo, "%s: %s!\n", CMfileName(argv[0]), argNum < 3 ? "Missing runoff" : "Extra arguments"); return (-1); } CMmsgPrint(CMmsgInfo, "Loading cells\n"); if ((network = NCRnetworkLoad(netName)) == (NCRnetwork_t *) NULL) return (-1); NCRroutingInitialize(network, (float) dt); CMmsgPrint(CMmsgInfo, "Opening runoff\n"); if ((runoff = NCRinputOpen(network, argv[1], "runoff")) == (void *) NULL) { NCRnetworkFree(network); return (-1); } if ((timeStepNum = NCRinputTimeStepNum(runoff)) == 0) { NCRnetworkFree(network); NCRinputClose(runoff); return (-1); } CMmsgPrint(CMmsgInfo, "Opening outflow\n"); if ((outflow = NCRoutputOpen(network, argv[2], "outflow")) == (void *) NULL) { NCRnetworkFree(network); return (-1); } CMmsgPrint(CMmsgInfo, "Start routing\n"); if (nThreads == 1) { for (timeStep = 0; timeStep < timeStepNum; ++timeStep) { NCRinputLoad(runoff, timeStep, network); for (timeHour = 0; timeHour < 24; timeHour += dt) for (cellId = 0; cellId < network->CellNum; cellId++) NCRroutingFunc((void *) network, (void *) NULL, cellId); if (NCRoutputWrite(outflow, timeStep, network) != true) goto Stop; } } else { size_t taskId, dLink; NCRnetworkCell_t *cell; CMthreadTeam_p team = CMthreadTeamCreate(nThreads); CMthreadJob_p job; if ((job = CMthreadJobCreate(team, (void *) network, network->CellNum, NCRroutingFunc)) == (CMthreadJob_p) NULL) { CMmsgPrint(CMmsgAppError, "Job creation error in %s:%d\n", __FILE__, __LINE__); CMthreadTeamDestroy(team); goto Stop; } for (cellId = 0; cellId < network->CellNum; cellId++) { cell = network->Cells[cellId]; taskId = network->CellNum - cellId - 1; dLink = cell->ToCellId > 0 ? network->CellNum - cell->ToCellId : taskId; CMthreadJobTaskDependent(job, taskId, dLink); } for (timeStep = 0; timeStep < timeStepNum; ++timeStep) { NCRinputLoad(runoff, timeStep, network); for (timeHour = 0; timeHour < 24; timeHour += dt) CMthreadJobExecute(team, job); if (NCRoutputWrite(outflow, timeStep, network) != true) goto Stop; } CMthreadJobDestroy(job); CMthreadTeamDestroy(team); } NCRoutputCopyInput(runoff, outflow); NCRinputClose(runoff); Stop: NCRoutputClose(outflow); NCRnetworkFree(network); return (0); }
int MFModelRun (int argc, char *argv [], int argNum, int (*conf) ()) { int i, iFunc, varID, dlink, taskId; char *timeCur, timeWritten [MFDateStringLength]; MFVariable_t *var; time_t sec; size_t threadsNum = CMthreadProcessorNum (); CMthreadTeam_p team = threadsNum > 0 ? CMthreadTeamCreate (threadsNum) : (CMthreadTeam_p) NULL; CMthreadJob_p job = (CMthreadJob_p) NULL; if (_MFModelParse (argc,argv,argNum,conf) == CMfailed) return (CMfailed); timeCur = MFDateGetCurrent (); time(&sec); CMmsgPrint (CMmsgInfo, "Model run started at... %s started at %.24s", timeCur, ctime(&sec)); if (_MFModelReadInput (timeCur) == MFStop) { CMmsgPrint (CMmsgInfo, "MFModelReadInput(%s) returned MFStop in: %s:%d",timeCur,__FILE__,__LINE__); return (CMfailed); } if (team != (CMthreadTeam_p) NULL) { if ((job = CMthreadJobCreate (team, (void *) NULL, _MFDomain->ObjNum, _MFUserFunc)) == (CMthreadJob_p) NULL) { CMmsgPrint (CMmsgAppError, "Job creation error in %s:%d",__FILE__,__LINE__); CMthreadTeamDestroy (team); return (CMfailed); } for (i = 0;i < _MFDomain->ObjNum; ++i) { dlink = _MFDomain->Objects [i].DLinkNum == 1 ? _MFDomain->Objects [i].DLinks [0] : i; dlink = _MFDomain->ObjNum - dlink - 1; taskId = _MFDomain->ObjNum - i - 1; CMthreadJobTaskDependent (job, taskId, dlink); } do { CMmsgPrint (CMmsgDebug, "Computing: %s", timeCur); CMthreadJobExecute (team, job); for (var = MFVarGetByID (varID = 1);var != (MFVariable_t *) NULL;var = MFVarGetByID (++varID)) { if (var->OutStream != (MFDataStream_t *) NULL) { if (var->State != true) MFDataStreamWrite (var, timeCur); } } } while ((timeCur = MFDateAdvance ()) != (char *) NULL ? _MFModelReadInput (timeCur) : MFStop); CMthreadJobDestroy (job); CMthreadTeamDestroy (team); } else // TODO Single CPU do { int link, uLink; double value; CMmsgPrint (CMmsgDebug, "Computing: %s", timeCur); for (var = MFVarGetByID (varID = 1);var != (MFVariable_t *) NULL;var = MFVarGetByID (++varID)) if (var->Route) { for (i = 0;i < _MFDomain->ObjNum; ++i) MFVarSetFloat (varID, i, 0.0); } for (i = _MFDomain->ObjNum - 1;i >= 0; --i) { for (var = MFVarGetByID (varID = 1);var != (MFVariable_t *) NULL;var = MFVarGetByID (++varID)) { if (var->Route) { value = 0.0; for (link = 0; link < _MFDomain->Objects [i].ULinkNum; ++link) { uLink = _MFDomain->Objects [i].ULinks [link]; value += MFVarGetFloat (varID,uLink,0.0); } MFVarSetFloat (varID, i, value); } } for (iFunc = 0;iFunc < _MFFunctionNum; ++iFunc) (_MFFunctions [iFunc]) (i); } for (var = MFVarGetByID (varID = 1);var != (MFVariable_t *) NULL;var = MFVarGetByID (++varID)) { if (var->OutStream != (MFDataStream_t *) NULL) { if (var->State != true) MFDataStreamWrite (var, timeCur); } strcpy (timeWritten,timeCur); } } while ((timeCur = MFDateAdvance ()) != (char *) NULL ? _MFModelReadInput (timeCur) : MFStop); for (var = MFVarGetByID (varID = 1);var != (MFVariable_t *) NULL;var = MFVarGetByID (++varID)) { if (var->InStream != (MFDataStream_t *) NULL) MFDataStreamClose (var->InStream); if (var->OutStream != (MFDataStream_t *) NULL) { if (var->State) MFDataStreamWrite (var, timeWritten); MFDataStreamClose (var->OutStream); } free (var->Data); } return (CMsucceeded); }
DBObjData *Compute(char *title, CMthreadUserExecFunc userFunc) { DBInt i, layerID, dataLayerID; size_t threadId; DBPosition pos; DBCoordinate coord; char *layerName; DBObjData *data; DBObjRecord *record; size_t threadsNum = CMthreadProcessorNum(); CMthreadTeam_t team; CMthreadJob_p job = (CMthreadJob_p) NULL; if (CMthreadTeamInitialize(&team, threadsNum) == (CMthreadTeam_p) NULL) { CMmsgPrint (CMmsgUsrError,"Team initialization error %s, %d",__FILE__,__LINE__); return ((DBObjData *) NULL); } if ((data = DBGridCreate(title, Extent, CellSize)) == (DBObjData *) NULL) return ((DBObjData *) NULL); data->Projection(GrdVar[0]->Projection()); // Taking projection from first grid variable GridIF = new DBGridIF(data); if (team.ThreadNum > 0) { if ((job = CMthreadJobCreate(GridIF->RowNum() * GridIF->ColNum(), userFunc, (void *) this)) == (CMthreadJob_p) NULL) { CMmsgPrint(CMmsgAppError, "Job creation error in %s:%d", __FILE__, __LINE__); CMthreadTeamDestroy(&team); return ((DBObjData *) NULL); } for (threadId = 0; threadId < team.ThreadNum; ++threadId) if (Table->Add("TEMPRecord") == (DBObjRecord *) NULL) { CMthreadTeamDestroy(&team); return ((DBObjData *) NULL); } } else { if ((record = Table->Add("TEMPRecord")) == (DBObjRecord *) NULL) return ((DBObjData *) NULL); } for (layerID = 0; layerID < LayerNum; ++layerID) { layerName = GrdVar[MasterVar]->CurrentLayer(layerID); for (i = 0; i < (DBInt) VarNum; ++i) { if ((dataLayerID = GrdVar[i]->FindLayer(layerName)) != DBFault) GrdVar[i]->CurrentLayer(dataLayerID); else { if (GrdVar[i]->LayerIsDated(0)) continue; GrdVar[i]->CurrentLayer(dataLayerID); } } if (layerID > 0) GridIF->AddLayer((char *) "New Layer"); LayerRec = GridIF->Layer(layerID); GridIF->RenameLayer(LayerRec, layerName); if (job != (CMthreadJob_p) NULL) CMthreadJobExecute(&team, job); else for (pos.Row = 0; pos.Row < GridIF->RowNum(); ++pos.Row) for (pos.Col = 0; pos.Col < GridIF->ColNum(); ++pos.Col) { GridIF->Pos2Coord(pos, coord); for (i = 0; i < (DBInt) VarNum; ++i) GrdVar[i]->GetVariable(record, coord); for (i = 0; i < (DBInt) ExpNum; ++i) Expressions[i]->Evaluate(record); GridIF->Value(LayerRec, pos, Operand->Float(record)); } GridIF->RecalcStats(LayerRec); } delete GridIF; CMthreadTeamDestroy(&team); return (data); }