view example/send_args/main.cc @ 2050:26dd777ba95d draft

add User API
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Thu, 28 Jan 2016 15:43:36 +0900
parents 3babb36ac459
children
line wrap: on
line source

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

extern void task_init(void);

static int spe_num = 1;
static int task_num = 1;
static CPU_TYPE spe_cpu = SPE_ANY;

extern TaskManager *manager;
const char *usr_help_str = "Usage: ./string_args [-cpu cpu_num] [-sw send_word] [-t task_num]\n";

static char*
init(int argc, char **argv)
{
    char *send_word = 0;

    for (int i = 1; argv[i]; ++i) {
        if (strcmp(argv[i], "-cpu") == 0) {
            spe_num = atoi(argv[i+1]); i++;
        } else if (strcmp(argv[i], "-sw") == 0) {
            send_word = argv[i+1]; i++;
        } else if (strcmp(argv[i], "-t") == 0) {
            task_num = atoi(argv[i+1]); i++;
        }
    }

    if (send_word==0){
        puts(usr_help_str);
        exit(1);
    }

    return send_word;
}

static void
run_start(TaskManager *manager,char *send_word,int send_word_len)
{
    HTask *exec;
    int *intTable = (int*)manager->allocate(256 * sizeof(int));

    for(int a = 0; a < 256; a++){
        intTable[a] = 10 * a;
    }

    for(int i = 0; i < task_num; i++){

        exec = manager->create_task(TASK_EXEC);
        exec->set_cpu(spe_cpu);
        exec->set_inData(0,send_word,send_word_len + 1);
        exec->set_inData(1,intTable,256);

        exec->set_param(0,(long)i);
        exec->spawn();
    }
}


int
TMmain(TaskManager *manager, int argc, char *argv[])
{
    const char *send_word = init(argc,argv);
    int send_word_len = strlen(send_word);

    char *sword = (char*)manager->allocate(send_word_len + 1);
    memcpy(sword,send_word,send_word_len + 1); // to get correct alignment

    task_init();
    run_start(manager, sword, send_word_len);

    return 0;
}