Mercurial > hg > Game > Cerium
view example/multiply/main.cc @ 2069:26aa08c9a1de draft default tip
cuda example fix
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 12 Feb 2017 10:04:55 +0900 |
parents | 273de551f726 |
children |
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); extern void gpu_task_init(void); static int task = 1; static int length = DATA_NUM; static CPU_TYPE spe_cpu = SPE_ANY; const char *usr_help_str = "Usage: ./multiply \n"; static int print_flag = 0; void TMend(TaskManager *); float *A,*B,*C; 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"); if(print_flag == 1) { 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], "-t") == 0) { task = atoi(argv[++i]); } else if (strcmp(argv[i], "-print") == 0) { print_flag = 1; } else if (strcmp(argv[i], "-any") == 0) { spe_cpu = ANY_ANY; } else if (strcmp(argv[i], "-g") == 0) { spe_cpu = GPU_0; } } } void multi_init(TaskManager *manager) { HTask* multiply; // HTask* hoge; 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+1)/10.f; } // set ND_RANGE parameter // 初期値は全部1になっている // ndr = new ND_RANGE_T; // ndr->gws[0] = length; // manager->set_NDRange(ndr); // for(int i=0;i<10;i++) { 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); multiply->set_param(0,(long)length); // param 0に0~length-1をsetしたtaskをlength個spawnする multiply->iterate(length); // hoge = manager->create_task(MULTIPLY_TASK); // hoge->set_cpu(spe_cpu); // hoge->set_inData(0,(memaddr)A,sizeof(float)*length); // hoge->set_inData(1,(memaddr)B,sizeof(float)*length); // hoge->set_inData(2,(memaddr)C,sizeof(float)*length); // hoge->set_outData(0,(memaddr)C,sizeof(float)*length); // hoge->set_param(0,(long)0); // hoge->wait_for(multiply); // hoge->iterate(length); //multiply->spawn(); // } } int TMmain(TaskManager *manager,int argc, char *argv[]) { init(argc, argv); // Task Register task_init(); gpu_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(); printf("Time: %0.6f\n",ed_time-st_time); check_data(); delete A; delete B; delete C; }