view example/multiply/main.cc @ 1613:0d368b575bb5 draft

fix not working yet
author Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
date Fri, 10 May 2013 00:41:58 +0900
parents 806b4658ced6
children 05d449e7b9f8
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "TaskManager.h"
#include "Func.h"

extern void task_init(void);
static int task = 1;
static int length = DATA_NUM;
CPU_TYPE spe_cpu = SPE_ANY;
const char *usr_help_str = "Usage: ./multiply \n";
void TMend(TaskManager *);

float *A,*B,*C;
ND_RANGE_T_PTR ndr;

static double st_time = 0 ;
static double ed_time = 0;

static double
getTime() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec + (double)tv.tv_usec*1e-6;
}

static void
check_data() {
    for(int i=0; i<length;i++) {
        if (A[i]*B[i]!=C[i]) {
            printf("Multiplication went wrong.\n");
            return;
        }
    }
    printf("Multiplication was succeeded.\n");
    return;
}
        
static void
print_result() {
    printf("---\n");
    for (int i =0;i<length;i++) {
        printf("%f * %f = %f \n",A[i],B[i],C[i]);
    }
    printf("---\n");
}


void
init(int args, char *argv[]) {
    for (int i = 1; argv[i]; ++i) {
        if (strcmp(argv[i], "--length") == 0 || strcmp(argv[i], "-l") == 0) {
            length = atoi(argv[++i]);
        }else if (strcmp(argv[i], "-g") == 0) {
            spe_cpu = GPU_0 ;
        } else if (strcmp(argv[i], "-t") == 0) {
            task = atoi(argv[++i]);
        }
    }
}

void
multi_init(TaskManager *manager)
{
    HTask *multiply;
    
    A = new float[length];
    B = new float[length];
    C = new float[length];
    for(int i=0; i<length; i++) {
        A[i]=(float)(i+1000);
        B[i]=(float)i/10.f;
    }

    // set ND_RANGE parameter
    // 初期値は全部1になっている
    //    ndr = new ND_RANGE_T;
    
    //    ndr->gws[0] = length;
    //    manager->set_NDRange(ndr);

    multiply = manager->create_task(MULTIPLY_TASK);
    multiply->set_cpu(spe_cpu);

    /**
     * Set of Input Data
     *   add_inData(address of input data, size of input data);
     * 単一のtaskを渡してscheduler側でiterをまわした方がよい
     */
    multiply->set_inData(0,(memaddr)A, sizeof(float)*length);
    multiply->set_inData(1,(memaddr)B, sizeof(float)*length);

    /**
     * Set of OutPut area
     *   add_outData(address of output area, size of output area);
     */
    multiply->set_outData(0,(memaddr)C, sizeof(float)*length);

    // param 0に0~length-1をsetしたtaskをlength個spawnする
    multiply->iterate(length); 
    
}


int
TMmain(TaskManager *manager,int argc, char *argv[])
{
    init(argc, argv);
    // Task Register
    task_init();
    for (int i = 0; i < task; ++i) {
        multi_init(manager);
    }
    st_time = getTime();
    manager->set_TMend(TMend);
    return 0;
}

void
TMend(TaskManager *manager)
{
    ed_time = getTime();
    print_result();
    check_data();
    printf("Time: %0.6f\n",ed_time-st_time);

    delete A;
    delete B;
    delete C;
    delete ndr;
}