int main(int argc, const char* argv[])
{
    int rc;
    SeparationFlags sf;
    SeparationPrefs preferences;
    const char** argvCopy = NULL;

  #ifdef NDEBUG
    mwDisableErrorBoxes();
  #endif /* NDEBUG */

    argvCopy = mwFixArgv(argc, argv);
    rc = parseParameters(argc, argvCopy ? argvCopy : argv, &sf);
    if (rc)
    {
        if (BOINC_APPLICATION)
        {
            freeSeparationFlags(&sf);
            mwBoincInit(MW_PLAIN);
            parseParameters(argc, argvCopy, &sf);
            printVersion(TRUE, FALSE);
        }

        mw_printf("Failed to parse parameters\n");
        free(argvCopy);
        mw_finish(EXIT_FAILURE);
    }


    rc = separationInit(sf.debugBOINC);
    free(argvCopy);
    if (rc)
        return rc;

    separationReadPreferences(&preferences);
    setFlagsFromPreferences(&sf, &preferences, argv[0]);

    if (sf.processPriority != MW_PRIORITY_INVALID)
    {
        mwSetProcessPriority(sf.processPriority);
    }

    rc = worker(&sf);

    freeSeparationFlags(&sf);

    if (!sf.ignoreCheckpoint && sf.cleanupCheckpoint && rc == 0)
    {
        mw_report("Removing checkpoint file '%s'\n", CHECKPOINT_FILE);
        mw_remove(CHECKPOINT_FILE);
    }

    mw_finish(rc);
    return rc;
}
Esempio n. 2
0
static void passiveMotionFunc(int x, int y)
{
    if (scene->fullscreen)
    {
        mw_finish(EXIT_SUCCESS);
    }
}
int main(int argc, const char* argv[])
{
    int rc;
    VisArgs flags;
    scene_t* scene = NULL;

    if (nbglBoincGraphicsInit(FALSE))
        return 1;

    if (nbglHandleVisArguments(argc, (const char**) argv, &flags))
    {
        freeVisArgs(&flags);
        return 1;
    }

    if (!flags.file)
    {
        scene = nbglConnectSharedScene(flags.instanceId);
        if (!scene)
        {
            freeVisArgs(&flags);
            return 1;
        }

        if (nbglCheckConnectedVersion(scene) || nbglGetExclusiveSceneAccess(scene))
        {
            freeVisArgs(&flags);
            return 1;
        }

        mw_report("Process %d acquired instance id %d\n", (int) getpid(), flags.instanceId);

        nbglInstallExitHandlers();
        g_scene = scene;
    }
    else
    {
        scene = nbglLoadStaticSceneFromFile(flags.file);
        if (!scene)
        {
            freeVisArgs(&flags);
            return 1;
        }
    }

    rc = nbglRunGraphics(scene, &flags);

    if (flags.file)
    {
        free(scene);
    }

    freeVisArgs(&flags);
    mw_finish(rc);

    return rc;
}
int main(int argc, char** argv)
{
    int retval = 0;

#if BOINC_APP_GRAPHICS
  #if defined(_WIN32) || defined(__APPLE__)
      retval = boinc_init_graphics(worker);
  #else
      retval = boinc_init_graphics_lib(worker, argv[0]);
  #endif /*  defined(_WIN32) || defined(__APPLE__) */

  if (retval)
      exit(retval);
#endif /* BOINC_APP_GRAPHICS */

#if defined(_WIN32) && COMPUTE_ON_GPU
    //make the windows GPU app have a higher priority
    BOINC_OPTIONS options;
    boinc_options_defaults(options);
    options.normal_thread_priority = 1; // higher priority (normal instead of idle)
    retval = boinc_init_options(&options);
#else
    /* TODO: for release build, use the boinc defaults*/
    //retval = boinc_init();
    retval = boinc_init_diagnostics(  BOINC_DIAG_DUMPCALLSTACKENABLED
                                    | BOINC_DIAG_HEAPCHECKENABLED
                                    | BOINC_DIAG_MEMORYLEAKCHECKENABLED);
#endif /* defined(_WIN32) && COMPUTE_ON_GPU */
    if (retval)
        exit(retval);


#if COMPUTE_ON_GPU
    //Choose the GPU to execute on, first look
    //at the command line argument for a
    //--device 0..n string, then enumerate all CUDA
    //devices on the system and choose the one
    //with double precision support and the most
    //GFLOPS
    //APP_INIT_DATA init_data;
    //boinc_get_init_data_p(&init_data);
    char* project_prefs = NULL;  //init_data.project_preferences;
    if (choose_gpu(argc, argv) == -1)
    {
        fprintf(stderr, "Unable to find a capable GPU\n");
        mw_finish(EXIT_FAILURE);
    }
    MW_DEBUGMSG("got here\n");
    parse_prefs(project_prefs);
#endif /* COMPUTE_ON_GPU */

    worker(argc, (const char**) argv);

    return retval;
}
Esempio n. 5
0
static void mouseFunc(int button, int state, int x, int y)
{
    if (scene->fullscreen)
    {
        mw_finish(EXIT_SUCCESS);
    }

    if (state == GLUT_DOWN)
    {
        xlast = x;
        ylast = y;

        if (glutGetModifiers() == GLUT_ACTIVE_SHIFT)
        {
            scene->mousemode = 2;
        }
        else
        {
            scene->mousemode = 1;
        }
    }
}
Esempio n. 6
0
int main(int argc, const char* argv[])
{
    NBodyFlags nbf = EMPTY_NBODY_FLAGS;
    int rc = 0;

    specialSetup();

    if (readParameters(argc, argv, &nbf))
        exit(EXIT_FAILURE);
    free(argvCopy);

    if (nbodyInit(&nbf))
    {
        exit(EXIT_FAILURE);
    }

    nbodyPrintVersion();
    setDefaultFlags(&nbf);
    setNumThreads(nbf.numThreads);

    if (nbf.verifyOnly)
    {
        rc = verifyFile(&nbf);
    }
    else
    {
        rc = runNBodySimulation(&nbf);
        if (nbf.cleanCheckpoint)
        {
            mw_report("Removing checkpoint file '%s'\n", nbf.checkpointFileName);
            mw_remove(nbf.checkpointFileName);
        }
    }

    freeNBodyFlags(&nbf);
    mw_finish(rc);

    return rc;
}
int main(int argc, const char* argv[])
{
    NBodyFlags nbf;
    int rc = 0;
    const char** argvCopy = mwFixArgv(argc, argv);

    nbSpecialSetup();

    if (nbReadParameters(argc, argvCopy ? argvCopy : argv, &nbf))
    {
        if (BOINC_APPLICATION)
        {
            mwBoincInit(MW_PLAIN);
            nbReadParameters(argc, argvCopy ? argvCopy : argv, &nbf);
            nbPrintVersion(TRUE, FALSE);
        }

        mw_finish(EXIT_FAILURE);
    }

    if (nbInit(&nbf))
    {
        exit(EXIT_FAILURE);
    }

    if (BOINC_APPLICATION && mwIsFirstRun())
    {
        nbPrintVersion(TRUE, FALSE);
    }

    nbSetDefaultFlags(&nbf);
    if (nbSetNumThreads(nbf.numThreads))
    {
        mw_finish(EXIT_FAILURE);
    }

    if (nbf.verifyOnly)
    {
        rc = nbVerifyFile(&nbf);
    }
    else if (nbf.matchHistogram)
    {
        double emd;

        emd = nbMatchHistogramFiles(nbf.histogramFileName, nbf.matchHistogram);
        mw_printf("%.15f\n", emd);
        rc = isnan(emd);
    }
    else
    {
        rc = nbMain(&nbf);
        rc = nbStatusToRC(rc);

        if (!nbf.noCleanCheckpoint)
        {
            mw_report("Removing checkpoint file '%s'\n", nbf.checkpointFileName);
            mw_remove(nbf.checkpointFileName);
        }
    }

    fflush(stderr);
    fflush(stdout); /* Odd things happen with the OpenCL one where stdout starts disappearing */


    freeNBodyFlags(&nbf);

    if (BOINC_APPLICATION)
    {
        mw_finish(rc);
    }

    return rc;
}
void launchVisualizer(NBodyState* st, const char* visArgs)
{
    pid_t pid;
    char* path = NULL;
    char* newPath = NULL;
    int argc = 0;
    char* buf = NULL;
    char* p = NULL;
    char** argv = NULL;
    size_t argvSize = 0;
    size_t visArgsLen = 0;

    if (!st->scene) /* If there's no scene to share, there's no point */
        return;

    pid = fork();
    if (pid != 0)  /* Parent */
        return;

    /* Child */

    /* Hack to close the shared memory access we inherit so we
     * don't count it when the visualizer actually opens it again */
    if (detachSharedScene(st))
    {
        warn("Error detaching child from shared");
        return;
    }

    /* Put places convenient for testing. Not essential, failure of
     * any of these is OK */
    path = getenv("PATH");
    if (!path)
    {
        perror("Error getting PATH");
    }
    else
    {
        if (asprintf(&newPath, ".:../bin/:%s", path) < 0)
        {
            perror("Appending to path");
        }
        else
        {
            if (setenv("PATH", newPath, TRUE) < 0)
            {
                perror("Error setting PATH");
            }
            free(newPath);
        }
    }

    /* Stick the program name at the head of the arguments passed in */
    visArgsLen = visArgs ? strlen(visArgs) : 0;
    argvSize = visArgsLen + sizeof(nbodyGraphicsName) + 2; /* arguments + program name + space + null */
    buf = mwCalloc(argvSize, sizeof(char));

    p = stpcpy(buf, nbodyGraphicsName);
    p = stpcpy(p, " ");
    if (visArgs)
    {
        stpcpy(p, visArgs);
    }

    if (poptParseArgvString(buf, &argc, (const char***) &argv))
    {
        free(buf);
        free(path);
        warn("Error parsing arguments for visualizer '%s'\n", visArgs);
        return;
    }

    if (execvp(argv[0], argv) < 0)
    {
        perror("Failed to launch visualizer");
    }

    free(buf);
    free(argv);

    mw_finish(EXIT_SUCCESS);  /* Unnecessary */
}
/* Returns the newly allocated array of parameters */
static double* parse_parameters(int argc, const char** argv, int* paramnOut)
{
    poptContext context;
    int o;
    unsigned int i, paramn = 0;
    double* parameters = NULL;
    static const char** rest;

    static const struct poptOption options[] =
    {
        {
            "boinc-init-graphics", 'b',
            POPT_ARG_STRING, &boinc_graphics,
            'b', "Argument to boinc_init_graphics", NULL
        },

        {
            "search-parameter-file", 's',
            POPT_ARG_STRING, &search_parameter_file,
            's', "Search parameter file name", NULL
        },

        {
            "star-points-file", 'p',
            POPT_ARG_STRING, &star_points_file,
            'p', "Star points files", NULL
        },

        {
            "astronomy-parameter-file", 'a',
            POPT_ARG_STRING, &astronomy_parameter_file,
            'a', "Astronomy parameter file", NULL
        },

        {
            "output", 'o',
            POPT_ARG_STRING, &output_file,
            'o', "Output file", NULL
        },

        POPT_AUTOHELP

        { NULL, 0, 0, NULL, 0, NULL, NULL }
    };

    context = poptGetContext(argv[0],
                             argc,
                             argv,
                             options,
                             POPT_CONTEXT_POSIXMEHARDER);

    while ( ( o = poptGetNextOpt(context)) >= 0 );

    if ( o < -1 )
    {
        poptPrintHelp(context, stderr, 0);
        mw_finish(EXIT_FAILURE);
    }

    MW_DEBUG("Got arguments: "
             "boinc_graphics = '%s' "
             "search_parameter_file = '%s' "
             "star_points_file = '%s' "
             "astronomy_parameter_file = '%s' "
             "output_file = '%s'\n",
             boinc_graphics,
             search_parameter_file,
             star_points_file,
             astronomy_parameter_file,
             output_file);

    rest = poptGetArgs(context);
    if (rest)
    {
        while (rest[++paramn]);  /* Count number of parameters */

        MW_DEBUG("%u arguments leftover\n", paramn);

        parameters = (double*) malloc(sizeof(double) * paramn);

        errno = 0;
        for ( i = 0; i < paramn; ++i )
        {
            parameters[i] = strtod(rest[i], NULL);

            if (errno)
            {
                perror("error parsing command line parameters");
                poptPrintHelp(context, stderr, 0);
                free(parameters);
                poptFreeContext(context);
                mw_finish(EXIT_FAILURE);
            }
        }
    }

    poptFreeContext(context);

    *paramnOut = paramn;
    return parameters;
}
static void worker(int argc, const char** argv)
{
    double* parameters;
    int ret1, ret2;
    int number_parameters, ap_number_parameters;
    ASTRONOMY_PARAMETERS ap = { 0 };
    STAR_POINTS sp = { 0 };
    EVALUATION_STATE es = { 0 };

    parameters = parse_parameters(argc, argv, &number_parameters);

    if (!parameters)
    {
        fprintf(stderr, "Could not parse parameters from the command line\n");
        mw_finish(EXIT_FAILURE);
    }

    ret1 = read_astronomy_parameters(astronomy_parameter_file, &ap);
    ret2 = read_star_points(star_points_file, &sp);

    MW_DEBUG("ap.number_stream_parameters = %d\n", ap.number_stream_parameters);

    if (ret1)
    {
        fprintf(stderr,
                "APP: error reading astronomy parameters from file %s: %d\n",
                astronomy_parameter_file,
                ret1);
    }

    if (ret2)
    {
        fprintf(stderr,
                "APP: error reading star points from file %s: %d\n",
                star_points_file,
                ret2);
    }

    if (ret1 | ret2)
    {
        free(parameters);
        cleanup_worker();
		mw_finish(EXIT_FAILURE);
    }

    initialize_state(&ap, &sp, &es);

    ap_number_parameters = get_optimized_parameter_count(&ap);

    if (number_parameters < 1 || number_parameters != ap_number_parameters)
    {
        fprintf(stderr,
                "Error reading parameters: number of parameters from the "
                "command line (%d) does not match the number of parameters "
                "to be optimized in %s (%d)\n",
                number_parameters,
                astronomy_parameter_file,
                ap_number_parameters);

        free(parameters);
        cleanup_worker();
        mw_finish(EXIT_FAILURE);
    }

    set_astronomy_parameters(&ap, parameters);

#if COMPUTE_ON_CPU
    init_constants(&ap);
    init_simple_evaluator(cpu_evaluate);
#elif USE_CUDA
    init_constants(&ap);
    init_simple_evaluator(cuda_evaluate);
#elif USE_OCL
    init_constants(&ap);
    init_simple_evaluator(ocl_evaluate);
#else
    #error "Must choose CUDA, OpenCL or CPU"
#endif /* COMPUTE_ON_CPU */

    /* CHECKME: What is this magic 3.0, and why was it being
     * subtracted from CPU and CUDA result, but not OpenCL? */
    double likelihood = evaluate(parameters, &ap, &es, &sp) - 3.0;

    fprintf(stderr, "<search_likelihood> %0.20f </search_likelihood>\n", likelihood);
    fprintf(stderr, "<search_application> %s %s </search_application>\n", BOINC_APP_VERSION, PRECISION);

    free(parameters);
	cleanup_worker();

    mw_finish(EXIT_SUCCESS);

}
void nbLaunchVisualizer(NBodyState* st, const char* visArgs)
{
    pid_t pid;
    const char* path = NULL;
    char* newPath = NULL;
    int argc = 0;
    char* buf = NULL;
    char* p = NULL;
    char** argv = NULL;
    size_t argvSize = 0;
    size_t visArgsLen = 0;
    char idArg[128];

    if (!st->scene) /* If there's no scene to share, there's no point */
        return;

    if (st->usesExact)
    {
        mw_printf("Visualizer broken with Exact\n");
        return;
    }

    pid = fork();
    if (pid != 0)  /* Parent */
        return;

    /* Child */

    /* Put places convenient for testing. Not essential, failure of
     * any of these is OK */
    path = getenv("PATH");
    if (!path)
    {
        mwPerror("Error getting PATH");
    }
    else
    {
        if (asprintf(&newPath, ".:../bin/:%s", path) < 0)
        {
            mwPerror("Appending to path");
        }
        else
        {
            if (setenv("PATH", newPath, TRUE) < 0)
            {
                mwPerror("Error setting PATH");
            }
            free(newPath);
        }
    }

    if (snprintf(idArg, sizeof(idArg), "--instance-id=%d ", st->scene->instanceId) == sizeof(idArg))
        mw_panic("Buffer too small for --instance-id visualizer argument\n");

    /* Stick the program name at the head of the arguments passed in */
    visArgsLen = visArgs ? strlen(visArgs) : 0;
    argvSize = visArgsLen + sizeof(idArg) + sizeof(nbodyGraphicsName) + 2; /* arguments + program name + space + null */
    buf = mwCalloc(argvSize, sizeof(char));

    p = stpcpy(buf, nbodyGraphicsName);
    p = stpcpy(p, " ");
    p = stpcpy(p, idArg);
    if (visArgs)
    {
        stpcpy(p, visArgs);
    }

    if (poptParseArgvString(buf, &argc, (const char***) &argv))
    {
        mw_printf("Error parsing arguments for visualizer '%s'\n", visArgs);
        free(buf);
        return;
    }

    if (execvp(argv[0], argv) < 0)
    {
        mwPerror("Failed to launch visualizer '%s'", argv[0]);
    }

    free(buf);
    free(argv);
    mw_finish(EXIT_SUCCESS);  /* Unnecessary */
}
Esempio n. 12
0
/* The function called whenever a key is pressed. */
static void keyPressed(unsigned char key, int x, int y)
{
    /* avoid thrashing this call */
    mwMilliSleep(THRASH_SLEEP_INTERVAL);

    if (scene->fullscreen)
    {
        mw_finish(EXIT_SUCCESS);
    }

    switch (key)
    {
        case ESCAPE:
        case 'q':
            glutDestroyWindow(window);
            mw_finish(EXIT_SUCCESS);

        case 'h':
            print_bindings(stderr);
            break;

        case 'p':
            scene->paused = !scene->paused;
            break;

        case ' ':
            scene->step = TRUE;
            break;

        case 'b':
            scene->starsize *= 1.1;
            if (scene->starsize > 100.0)
            {
                scene->starsize = 100.0;
            }
            scene->changed = TRUE;
            break;

        case 's':
            scene->starsize *= 0.9;
            if (scene->starsize < 1.0e-3)
            {
                scene->starsize = 1.0e-3;
            }
            scene->changed = TRUE;
            break;

        case 'm':
            scene->ntri *= 2;
            if (scene->ntri > 24)
            {
                scene->ntri = 24;
            }
            scene->changed = TRUE;
            break;

        case 'f':
            scene->ntri /= 2;
            if (scene->ntri < 4)
            {
                scene->ntri = 4;
            }
            scene->changed = TRUE;
            break;

        case 'a':
            scene->drawaxes = !scene->drawaxes;
            scene->changed = TRUE;
            break;
        case '>':
            scene->dt /= 2.0;
            if (scene->dt < 10.0)
            {
                scene->dt = 10.0;
            }
            break;
        case '<':
            scene->dt *= 2.0;
            if (scene->dt > 1.0e6)
            {
                scene->dt = 1.0e6;
            }
            break;

        default:
            break;
    }
}