CALresult getModuleNames(MWCALInfo* ci, SeparationCALNames* cn, CALuint numberStreams)
{
    CALresult err = CAL_RESULT_OK;
    CALuint i;
    char buf[20] = "";

    cn->outStreams = (CALname*) mwCalloc(numberStreams, sizeof(CALname));
    cn->inStreams = (CALname*) mwCalloc(numberStreams, sizeof(CALname));

    err |= getNameMWCALInfo(ci, &cn->nuBuf, "cb0");
    err |= getNameMWCALInfo(ci, &cn->sg_dx, "cb1");

    err |= getNameMWCALInfo(ci, &cn->rPts,  "i0");
    err |= getNameMWCALInfo(ci, &cn->rc,    "i1");
    err |= getNameMWCALInfo(ci, &cn->lTrig, "i2");
    err |= getNameMWCALInfo(ci, &cn->bTrig, "i3");

    err |= getNameMWCALInfo(ci, &cn->outBg, "o0");
    err |= getNameMWCALInfo(ci, &cn->inMu, "i4");
    for (i = 0; i < numberStreams; ++i)
    {
        sprintf(buf, "i%u", i + 5);
        err |= getNameMWCALInfo(ci, &cn->inStreams[i], buf);

        sprintf(buf, "o%u", i + 1);
        err |= getNameMWCALInfo(ci, &cn->outStreams[i], buf);
    }

    if (err != CAL_RESULT_OK)
        cal_warn("Failed to get module names", err);

    return err;
}
NBodyStatus nbInitNBodyStateCL(NBodyState* st, const NBodyCtx* ctx, const CLRequest* clr)
{
    cl_int err;

    /* Bodies must be set before trying to use this */
    if (!st->bodytab)
    {
        return NBODY_CONSISTENCY_ERROR;
    }

    if (ctx->potentialType == EXTERNAL_POTENTIAL_CUSTOM_LUA)
    {
        mw_printf("Cannot use Lua potential with OpenCL\n");
        return NBODY_UNSUPPORTED;
    }

    st->usesQuad = ctx->useQuad;
    st->usesExact = (ctx->criterion == Exact);
    st->usesCL = TRUE;

    st->ci = mwCalloc(1, sizeof(CLInfo));
    st->nbb = mwCalloc(1, sizeof(NBodyBuffers));
    st->workSizes = mwCalloc(1, sizeof(NBodyWorkSizes));
    st->kernels = mwCalloc(1, sizeof(NBodyKernels));

    err = mwSetupCL(st->ci, clr);
    if (err != CL_SUCCESS)
        return NBODY_CL_ERROR;

    if (!nbCheckDevCapabilities(&st->ci->di, ctx, st->nbody))
        return NBODY_CAPABILITY_ERROR;

    if (nbSetThreadCounts(st->workSizes, &st->ci->di, ctx) || nbSetWorkSizes(st->workSizes, &st->ci->di))
        return NBODY_ERROR;

    st->effNBody = nbFindEffectiveNBody(st->workSizes, st->usesExact, st->nbody);

    if (nbLoadKernels(ctx, st))
        return NBODY_CL_ERROR;

    err = nbCreateBuffers(ctx, st);
    if (err != CL_SUCCESS)
        return NBODY_CL_ERROR;

    err = nbSetInitialTreeStatus(st);
    if (err != CL_SUCCESS)
        return NBODY_CL_ERROR;

    err = nbSetAllKernelArguments(st);
    if (err != CL_SUCCESS)
        return NBODY_CL_ERROR;

    err = nbMarshalBodies(st, CL_TRUE);
    if (err != CL_SUCCESS)
        return NBODY_CL_ERROR;

    return NBODY_SUCCESS;
}
/* static function declaration */
static size_t emdAllocateStateBuffer(EMDState* state, int size1, int size2, int dims)
{
    size_t bufferSize;

    /* calculate buffer size */
    bufferSize = (size1 + 1) * (size2 + 1) * (sizeof(float) +   /* cost */
                 sizeof(char) +       /* is_x */
                 sizeof(float)) +     /* delta matrix */
                 (size1 + size2 + 2) * (sizeof(EMDNode2D) +   /* _x */
                                        sizeof(EMDNode2D*) +  /* cols_x & rows_x */
                                        sizeof(EMDNode1D) +   /* u & v */
                                        sizeof(float) +      /* s & d */
                                        sizeof(int) + sizeof(EMDNode2D*)) +    /* idx1 & idx2 */
                 (size1 + 1) * (sizeof(float*) + sizeof(char*) +     /* rows pointers for */
                                sizeof(float*)) + 256;               /*  cost, is_x and delta */

    if (bufferSize < dims * 2 * sizeof(float))
    {
        bufferSize = dims * 2 * sizeof(float);
    }


    state->buffer = mwCalloc(bufferSize, sizeof(char));

    return bufferSize;
}
/* Create a separate output buffer for each stream */
static CALresult createOutStreamBuffers(MWCALInfo* ci,
                                        SeparationCALMem* cm,
                                        const CALSeparationSizes* sizes)
{
    CALuint i, numberAllocated;
    CALresult err = CAL_RESULT_OK;

    cm->outStreams = (MWMemRes*) mwCalloc(cm->numberStreams, sizeof(MWMemRes));
    for (i = 0; i < cm->numberStreams; ++i)
    {
        err = createOutputBuffer2D(&cm->outStreams[i], ci, sizes->rSteps, sizes->muSteps);
        if (err != CAL_RESULT_OK)
        {
            cal_warn("Failed to create out streams buffer", err);
            break;
        }
    }

    /* Clean up if any failed before */
    numberAllocated = i;
    if (numberAllocated < cm->numberStreams)
    {
        for (i = 0; i < numberAllocated; ++i)
            releaseMWMemRes(ci->calctx, &cm->outStreams[i]);
        free(cm->outStreams);
    }

    return err;
}
NBodyStatus nbInitCL(NBodyState* st, const NBodyCtx* ctx, const CLRequest* clr)
{
    cl_int err;

    st->usesQuad = ctx->useQuad;
    st->usesExact = (ctx->criterion == Exact);
    st->usesCL = TRUE;
    st->useCLCheckpointing = clr->enableCheckpointing;

    st->ci = mwCalloc(1, sizeof(CLInfo));
    st->nbb = mwCalloc(1, sizeof(NBodyBuffers));
    st->workSizes = mwCalloc(1, sizeof(NBodyWorkSizes));
    st->kernels = mwCalloc(1, sizeof(NBodyKernels));

    err = mwSetupCL(st->ci, clr);
    if (err != CL_SUCCESS)
        return NBODY_CL_ERROR;

    return NBODY_SUCCESS;
}
static CALresult findCALChunks(const AstronomyParameters* ap,
                               const MWCALInfo* ci,
                               const CLRequest* clr,
                               const IntegralArea* ia,
                               SeparationCALChunks* chunks)
{
    CALresult err = CAL_RESULT_OK;
    CALuint i, nChunk;
    CALuint sum = 0;
    const CALuint nMod = 16; /* Keep chunks in sizes divisible by nMod */

    nChunk = deviceChunkEstimate(ap, ia, &ci->devAttribs, clr, &chunks->chunkWaitTime);
    if (nChunk == 0)
    {
        warn("Invalid number of chunks: %u\n", nChunk);
        return CAL_RESULT_ERROR;
    }
    else if (ia->mu_steps / nChunk < nMod)
    {
        chunks->nChunkMu = ia->mu_steps / nMod;
        warn("Warning: Estimated number of chunks (%u) too large. Using %u\n", nChunk, chunks->nChunkMu);
    }
    else
    {
        chunks->nChunkMu = nChunk;
    }

    chunks->chunkMuBorders = mwCalloc((chunks->nChunkMu + 1), sizeof(CALuint));
    //chunks->chunkRBorders = mwCalloc((chunks->nChunkR + 1), sizeof(CALuint));

    for (i = 0; i <= chunks->nChunkMu; ++i)
    {

        chunks->chunkMuBorders[i] = (i * ia->mu_steps + chunks->nChunkMu) / (chunks->nChunkMu * nMod);
        chunks->chunkMuBorders[i] *= nMod;
        if (chunks->chunkMuBorders[i] > ia->mu_steps)
            chunks->chunkMuBorders[i] = ia->mu_steps;

        if (i > 0)
            sum += chunks->chunkMuBorders[i] - chunks->chunkMuBorders[i - 1];
    }

    printChunks(ia, chunks);

    if (sum != ia->mu_steps)  /* Assert that the divisions aren't broken */
    {
        warn("Chunk mu steps does not match: %u != %u\n", sum, ia->mu_steps);
        free(chunks->chunkMuBorders);
        return CAL_RESULT_ERROR;
    }

    return err;
}
Exemple #7
0
void* aCallocA_ (size_t num, size_t size, const char *file, int line, const char *func)
{
#ifndef MEMWATCH
	void *ret = CALLOCA(num, size);
#else
	void *ret = mwCalloc(num, size, file, line);
#endif
	// ShowMessage("%s:%d: in func %s: calloc %d %d\n",file,line,func,num,size);
	if (ret == NULL){
		ShowFatalError("%s:%d: na funcao %s: erro de calculo, despejo de memoria!\n",file,line,func);
		exit(1);
	}
	return ret;
}
void nbLaunchVisualizer(NBodyState* st, const char* visArgs)
{
    PROCESS_INFORMATION pInfo;
    STARTUPINFO startInfo;
    size_t visArgsLen, argvSize;
    char* buf;

    (void) st;

    memset(&pInfo, 0, sizeof(pInfo));
    memset(&startInfo, 0, sizeof(startInfo));
    startInfo.cb = sizeof(startInfo);

    visArgsLen = visArgs ? strlen(visArgs) : 0;
    argvSize = visArgsLen + sizeof(nbodyGraphicsName) + 2; /* arguments + program name + space + null */
    buf = mwCalloc(argvSize, sizeof(char));

    strcat(buf, nbodyGraphicsName);
    strcat(buf, " ");
    if (visArgs)
    {
        strcat(buf, visArgs);
    }

    if (!CreateProcess(NULL,
                       buf,
                       NULL,
                       NULL,
                       FALSE,
                       NORMAL_PRIORITY_CLASS,
                       NULL,
                       NULL,
                       &startInfo,
                       &pInfo))
    {
        mw_printf("Error creating visualizer process: %ld\n", GetLastError());
    }

    free(buf);
}
static scene_t* nbglLoadStaticSceneFromFile(const char* filename)
{
    FILE* f;
    size_t lnCount; /* ~= nbody */
    size_t line = 0;
    int nbody = 0;
    char lnBuf[4096];
    int rc = 0;
    int ignore;
    double x, y, z;
    double vx, vy, vz;
    double lambda;
    FloatPos* r;
    int hasError = FALSE;
    scene_t* scene = NULL;

    f = fopen(filename, "r");
    if (!f)
    {
        mwPerror("Failed to open file '%s'", filename);
        return NULL;
    }

    lnCount = mwCountLinesInFile(f);
    if (lnCount == 0)
    {
        mw_printf("Error counting lines from file '%s'\n", filename);
        fclose(f);
        return NULL;
    }

    scene = mwCalloc(sizeof(scene_t) + 1 * (lnCount * sizeof(FloatPos)), sizeof(char));

    /* We don't need the buffering features so just use first buffer slot */
    r = &scene->queue.bodyData[0];
    scene->hasInfo = FALSE;
    scene->staticScene = TRUE;

    /* Make read data fake that we have 1 element in the queue */
    OPA_store_int(&scene->queue.head, 0);
    OPA_store_int(&scene->queue.tail, 1);


    readCoordinateSystem = FALSE;
    readHasGalaxy = FALSE;
    readCenterOfMass = FALSE;

    while (fgets(lnBuf, (int) sizeof(lnBuf), f) && line < lnCount)
    {
        ++line;

        if (strlen(lnBuf) + 1 >= sizeof(lnBuf))
        {
            mw_printf("Error reading histogram line "ZU" (Line buffer too small): %s", line, lnBuf);
            hasError = TRUE;
            break;
        }

        /* Skip comments and blank lines */
        if (lnBuf[0] == '#' || lnBuf[0] == '\n')
            continue;

        if (nbglTryReadSceneItems(lnBuf, scene))
            continue;

        rc = sscanf(lnBuf,
                    "%d , %lf , %lf , %lf , %lf , %lf , %lf ",
                    &ignore,
                    &x, &y, &z,
                    &vx, &vy, &vz);
        if (rc == 7)
        {
            /* May or may not be there */
            rc = sscanf(lnBuf, " , %lf \n", &lambda);
            if (rc != 1)
            {
                sscanf(lnBuf, " \n");
            }

            r[nbody].x = (float) x;
            r[nbody].y = (float) y;
            r[nbody].z = (float) z;

            ++nbody;
        }
        else
        {
            hasError = TRUE;
        }
    }

    if (fclose(f))
    {
        mwPerror("Failed to close file '%s'", filename);
    }

    if (!readCoordinateSystem || !readHasGalaxy || !readCenterOfMass)
    {
        mw_printf("Warning: Failed to read some scene info items\n");
    }

    scene->nbody = nbody;
    if (hasError)
    {
        free(scene);
        return NULL;
    }

    return scene;
}
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 */
}
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 */
}