Exemplo n.º 1
0
int main(int nArgc, char **ppArgv)
{
    CommandArgs args;
    CLSModule *pModule;

    if (ParseArgs(nArgc, ppArgv, &args) < 0) exit(1);

    if (args.pszFactoryUrl) SetFactoryUrl(args.pszFactoryUrl);
    if (args.pszSourcePath) SetSourcePath(args.pszSourcePath);
    if (args.pszLibraryPath) SetLibraryPath(args.pszLibraryPath);

    pModule = CompileCAR(args.pszSource, args.mAttribs);
    if (!pModule) exit(1);

    // generate file(s) specified by command line
    //
    if (args.pszCAR) {
        if (CLS2CAR(args.pszCAR, pModule) < 0) goto ErrorExit;
    }
    if (args.pszCLS) {
        if (SaveCLS(args.pszCLS, pModule) < 0) goto ErrorExit;
    }
    if (args.mAttribs & Command_E_GenExCLS) {
        if (ExtendCLS(pModule, TRUE) < 0) goto ErrorExit;
        if (SaveCLS(args.pszExCLS, pModule) < 0) goto ErrorExit;
    }

    DestroyAllLibraries();
    DestroyCLS(pModule);
    return 0;

ErrorExit:
    fprintf(stderr, "[ERROR] carc (0x1004) : Writing file failed, aborting.\n");
    DestroyCLS(pModule);
    exit(1);
}
Exemplo n.º 2
0
int Run(int argc, char ** argv) {
    int N = 10;
    int nbIterations = 10;
    std::string momentumPath, outputPath = "./", deviceName, sourcePath = "./OpenCL.cl";

    if (argc < 3) {
        usage();
        return 1;
    }

    auto image = ScalarField::Read({ argv[1] });
    auto target = ScalarField::Read({ argv[2] });

    argc -= 2;
    argv += 2;

    Matrix<4, 4> transfo;
    memset(&transfo[0], 0, 16 * sizeof(float));
    transfo[0][0] = transfo[1][1] = transfo[2][2] = transfo[3][3] = 1;

    std::array<float, 7> weights = {{ 100.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f }},
        sigmaXs = {{ 3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f }},
        sigmaYs = {{ 3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f }},
        sigmaZs = {{ 3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f }};

    float alpha = 0.001f, maxVeloUpdate = 0.5f;

    while (argc > 1) {
        auto arg = std::string { argv[1] };
        --argc; ++argv;
        if (arg == "-subdivisions" && argc > 1) {
            N = atoi(argv[1]);
            --argc; ++argv;
        } else if (arg == "-iterations" && argc > 1) {
            nbIterations = atoi(argv[1]);
            --argc; ++argv;
        } else if (arg == "-InputInitMomentum" && argc > 1) {
            momentumPath = argv[1];
            --argc; ++argv;
        } else if (arg == "-OutputPath" && argc > 1) {
            outputPath = argv[1];
            --argc; ++argv;
        } else if (arg == "-Device" && argc > 1) {
            deviceName = argv[1];
            --argc; ++argv;
        } else if (arg == "-ShowDevices") {
            for (auto && d : compute::system::devices())
                std::cout << d.name() << std::endl;
            return 0;
        } else if (arg == "-affineT" && argc > 12) {
            for (auto i = 1; i <= 12; ++i)
                transfo[(i - 1) / 4][(i - 1) % 4] = atof(argv[i]);
            argc -= 12;
            argv += 12;
        } else if (arg == "-Gauss" && argc > 1) {
            sigmaXs[0] = sigmaYs[0] = sigmaZs[0] = atof(argv[1]);
            --argc; ++argv;
        } else if (arg == "-M_gauss" && argc > 1) {
            auto temp = atoi(argv[1]);
            --argc; ++argv;
            for (auto i = 1; i <= 7; ++i) {
                if (temp >= i && argc > 2) {
                    weights[i - 1] = atof(argv[1]);
                    sigmaXs[i - 1] = sigmaYs[i - 1] = sigmaZs[i - 1] = atof(argv[2]);
                    argc -= 2;
                    argv += 2;
                }
            }
        } else if (arg == "-M_Gauss_easier" && argc > 2) {
            sigmaXs[0] = atof(argv[1]);
            sigmaXs[6] = atof(argv[2]);
            argc -= 2;
            argv += 2;

            if (sigmaXs[0] < sigmaXs[6]) {
                std::swap(sigmaXs[0], sigmaXs[6]);
            }

            sigmaYs[0] = sigmaZs[0] = sigmaXs[0];
            sigmaYs[6] = sigmaZs[6] = sigmaXs[6];

            weights[0] = 0.f;

            auto a = (sigmaYs[6] - sigmaYs[0]) / 6.f;
            auto b = sigmaYs[0] - a;

            for (auto i = 2; i <= 6; ++i)
                sigmaXs[i - 1] = sigmaYs[i - 1] = sigmaZs[i - 1] = i * a + b;
        } else if (arg == "-alpha" && argc > 1) {
            alpha = atof(argv[1]);
            --argc; ++argv;
        } else if (arg == "-MaxVeloUpdate" && argc > 1) {
            maxVeloUpdate = atof(argv[1]);
            --argc; ++argv;
        } else if (arg == "-KernelSource" && argc > 1) {
            sourcePath = argv[1];
            --argc; ++argv;
        } else {
            usage();
            return 1;
        }
    }

    ScalarField momentum;
    if (momentumPath.empty()) {
        momentum = ScalarField { image.NX(), image.NY(), image.NZ() };
        momentum.Fill(0.f);
    } else
        momentum = ScalarField::Read({ momentumPath.c_str() });


    if (deviceName.empty())
        SetDevice(compute::system::default_device());
    else
        SetDevice(compute::system::find_device(deviceName));

    std::cout << "OpenCL will use " << GetDevice().name() << std::endl;
    compute::command_queue queue { GetContext(), GetDevice() };
    SetSourcePath(std::move(sourcePath));

    GeoShoot gs { std::move(image), std::move(target), std::move(momentum), transfo, N, queue };

    gs.Weights = std::move(weights);
    gs.SigmaXs = std::move(sigmaXs);
    gs.SigmaYs = std::move(sigmaYs);
    gs.SigmaZs = std::move(sigmaZs);
    gs.Alpha = alpha;
    gs.MaxUpdate = maxVeloUpdate;

    gs.Run(nbIterations);
    queue.finish();

    gs.Save(outputPath);

    return 0;
}