view example/send_args/main.cc @ 1794:3d53b0ca02a2 draft

rename string_args to send_args
author Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
date Wed, 04 Dec 2013 18:01:17 +0900
parents example/string_args/main.cc@044b5bd12ef0
children 355304646b4b
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;
static int one_task_size = 4096 * 4;

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

static char*
init(int argc, char **argv)
{
    char *search_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) {
            search_word = argv[i+1]; i++;
        } else if (strcmp(argv[i], "-t") == 0) {
            task_num = atoi(argv[i+1]); i++;
        }
    }

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

    return search_word;
}

static void
run_start(TaskManager *manager,char *search_word,int search_word_len)
{
    HTask *exec;

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

        exec = manager->create_task(TASK_EXEC);
        exec->set_cpu(spe_cpu);
        exec->set_inData(0,search_word,search_word_len);

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


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

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

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

    return 0;
}