/* Maximum exit code is 255 which ruins everything even though we want * to have or'able errors. */ static int nbStatusToRC(NBodyStatus rc) { unsigned int n = (unsigned int) rc; unsigned int shift = 0; if (rc == NBODY_SUCCESS || (nbStatusIsWarning(rc) && !nbStatusIsFatal(rc))) { return 0; } while (n >> shift) { ++shift; } return (int) shift - 1; }
int nbMain(const NBodyFlags* nbf) { NBodyCtx* ctx = &_ctx; NBodyState* st = &_st; CLRequest clr; NBodyStatus rc = NBODY_SUCCESS; real ts = 0.0, te = 0.0; if (!nbOutputIsUseful(nbf)) { return NBODY_USER_ERROR; } nbSetCLRequestFromFlags(&clr, nbf); /* Find out what device we're using so we can tell the workunit * about it */ if (NBODY_OPENCL && !nbf->noCL) { rc = nbInitCL(st, ctx, &clr); if (nbStatusIsFatal(rc)) { destroyNBodyState(st); return rc; } } rc = nbResumeOrNewRun(ctx, st, nbf); if (nbStatusIsFatal(rc)) { destroyNBodyState(st); return rc; } nbSetCtxFromFlags(ctx, nbf); /* Do this after setup to avoid the setup clobbering the flags */ nbSetStateFromFlags(st, nbf); if (NBODY_OPENCL && !nbf->noCL) { rc = nbInitNBodyStateCL(st, ctx); if (nbStatusIsFatal(rc)) { destroyNBodyState(st); return rc; } } if (nbCreateSharedScene(st, ctx)) { mw_printf("Failed to create shared scene\n"); } if (nbf->visualizer && st->scene) { /* Make sure the first scene is available for the launched graphics */ nbForceUpdateDisplayedBodies(ctx, st); /* Launch graphics and make sure we are sure the graphics is * attached in case we are using blocking mode */ nbLaunchVisualizer(st, nbf->graphicsBin, nbf->visArgs); } if (nbf->reportProgress) { nbSetupCursesOutput(); } ts = mwGetTime(); rc = nbRunSystem(ctx, st); te = mwGetTime(); if (nbf->reportProgress) { nbCleanupCursesOutput(); } nbReportSimulationComplete(st); if (nbStatusIsFatal(rc)) { mw_printf("Error running system: %s (%d)\n", showNBodyStatus(rc), rc); destroyNBodyState(st); return rc; } else { if (nbStatusIsWarning(rc)) { mw_printf("System complete with warnings: %s (%d)\n", showNBodyStatus(rc), rc); } if (nbf->printTiming) { printf("<run_time> %f </run_time>\n", te - ts); } } rc = nbReportResults(ctx, st, nbf); destroyNBodyState(st); return rc; }