Do more. Code less. Free software for GPU computing.
<scroll to top>

examples/vortex/vortex.cpp

#include <stdio.h>
#include <string.h>

#include <arrayfire.h>
using namespace af;

static void vortex_gpu(bool console)
{
    // seconds to run demo
    double time_total = 10;

    // initialize parameters
    unsigned particles = 10000;
    float revolutions = 5.0;
    float xAx = 0.7f;
    float yAx = 0.3f;
    float zD = -0.0025f;

    float zero = 0;

    array Z = randu(particles);
    array T = randu(particles) * 2 * af::pi;

    array X = mul(mul(xAx, Z), cos(T)) + 0.5;
    array Y = mul(mul(yAx, Z), sin(T)) + 0.5;

    timer time_start, time_last;
    time_start = time_last = timer::tic();
    double max_rate = 0;
    unsigned iter = 0, iter_last = 0;

    while (true) {
        Z = Z + zD;

        // Remove old points and add new points
        Z = array(Z < zero) + mul(Z, array(Z >= zero));

        // Update the X and Y points
        array TEMP = T + 2 * af::pi * revolutions * .00025 * (iter++);
        X = xAx * mul(Z, cos(TEMP)) + 0.5;
        Y = yAx * mul(Z, sin(TEMP)) + 0.5;

        if (!console)
            points(X, Z, Y);

        double elapsed = timer::toc(time_last);
        if (elapsed > 1) {
            double rate = (iter - iter_last) / elapsed;
            double total_elapsed = timer::toc(time_start);
            time_last = timer::tic();
            iter_last = iter;
            max_rate = std::max(max_rate, rate);
            if (total_elapsed >= time_total)
                break;
            if (!console)
                printf("  iterations per second: %.0f   (progress %.0f%%)\n",
                       rate, 100.0f * total_elapsed / time_total);
        }
    }

    if (console)
        printf(" ### vortex_example: %f Iterations per second\n", max_rate);

}

int main(int argc, char* argv[])
{
    bool console = false;
    if (argc > 2 || (argc == 2 && strcmp(argv[1],"-"))) {
        printf("usage: vortex [-]\n");
        return -1;
    } else if (argc == 2 && !strcmp(argv[1],"-")) {
        console = true;
    }

    printf("simulation of a vortex of particles\n");
    try {
        vortex_gpu(console);
    } catch (af::exception& e) {
        fprintf(stderr, "%s\n", e.what());
    }

    #ifdef WIN32 // pause in Windows
    if (!console) {
        printf("hit [enter]...");
        getchar();
    }
    #endif
    return 0;
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines