Esempio n. 1
0
int
main(int argc, char *argv[])
{
    grpc_function_handle_t handles[2];
    grpc_sessionid_t ids[2];
    grpc_error_t result = GRPC_NO_ERROR;
    char *hosts[2];
    long count[2];
    double pi;
    long times, sum;
    int i;

    if (argc != 4){
        fprintf(stderr, "USAGE: %s TIMES HOSTNAME1 HOSTANME2\n", argv[0]);
        exit(2);
    }

    hosts[0] = NULL;
    hosts[0] = strdup(argv[2]);
    hosts[1] = NULL;
    hosts[1] = strdup(argv[3]);
    if ((hosts[0] == NULL) || (hosts[1] == NULL)) {
      fprintf(stderr,
	      "strdup: Can't allocate the storage for hostname.\n");
      exit(2);
    }

    times = atol(argv[1]) / 2;

    /* Initialize */
    result = grpc_initialize(config_file);
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_initialize() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    /* Initialize Function handles */
    for (i = 0; i < 2; i++) {
        result = grpc_function_handle_init(&handles[i], hosts[i],
            func_name);
        if (result != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_function_handle_init() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }      
    }

    /* Asynchronous call */
    for (i = 0; i < 2; i++) {
        result = grpc_call_async(&handles[i], &ids[i], i, times, &count[i]);
        if (result != GRPC_NO_ERROR){
            fprintf(stderr, "grpc_call_async() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }
    }

    /* wait for session */
    result = grpc_wait_all();
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_wait_all() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    /* Destruct Function handles */
    for (i = 0; i < 2; i++) {
        result = grpc_function_handle_destruct(&handles[i]);
        if (result != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_function_handle_destruct() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }
    }

    /* Compute and display pi. */
    for (i = 0, sum = 0; i < 2; i++) {
        sum += count[i];
    }
    pi = 4.0 * (sum / ((double) times * 2));
    printf("PI = %f\n", pi);

    /* Finalize */
    result = grpc_finalize();
    if (result != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_finalize() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    for (i = 0; i < 2; i++) {
        free(hosts[i]);
    }

    return 0;
}
Esempio n. 2
0
int
main(int argc, char *argv[])
{
    grpc_function_handle_t handle;
    grpc_sessionid_t id;
    grpc_error_t result = GRPC_NO_ERROR;
    char *host = NULL;
    long times, answer;

    if (argc != 3){
        fprintf(stderr, "USAGE: %s TIMES HOSTNAME\n", argv[0]);
        exit(2);
    }
    times = atol(argv[1]);
    host = argv[2];

    /* Initialize */
    result = grpc_initialize(config_file);
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_initialize() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    /* Initialize Function handles */
    result = grpc_function_handle_init(&handle, host, func_name);
    if (result != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_function_handle_init() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    /* Asynchronous call */
    result = grpc_call_async(&handle, &id, times, &answer);
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_call_async() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    /* wait for session */
    result = grpc_wait_all();
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_wait_all() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    /* Destruct Function handles */
    result = grpc_function_handle_destruct(&handle);
    if (result != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_function_handle_destruct() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    /* Compute and display pi. */
    printf("PI = %f\n", 4.0 * ((double)answer / times));

    /* Finalize */
    result = grpc_finalize();
    if (result != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_finalize() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    return 0;
}
Esempio n. 3
0
int
main(int argc, char *argv[])
{
    grpc_function_handle_t *handle;
    grpc_sessionid_t *id = NULL;
    grpc_error_t result = GRPC_NO_ERROR;
    char *host = NULL;
    int n;
    double scalarIn;
    double scalarOut;
    double *arrayIn;
    double *arrayOut;

    int i;
    int mismatchCount = 0;

    if (argc < 3){
        fprintf(stderr, "USAGE: %s HOSTNAME No_of_CPUs\n", argv[0]);
        exit(2);
    }

    host = strdup(argv[1]);
    n = atoi(argv[2]);

    /* prepare arg data */
    arrayIn = (double*)malloc(sizeof(double)*n);
    arrayOut = (double*)malloc(sizeof(double)*n);

    scalarIn = 1;
    for (i = 0; i < n; i++){
        arrayIn[i] = (double)i;
    }

    handle = (grpc_function_handle_t *)malloc(sizeof(grpc_function_handle_t));
    if (handle == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage for handles.\n");
        exit(2);
    }

    id = (grpc_sessionid_t *)malloc(sizeof(grpc_sessionid_t));
    if (id == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage.\n");
        exit(2);
    }

    /* Initialize */
    result = grpc_initialize(config_file);
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_initialize() error. (%s)\n",
        grpc_error_string(result));
        exit(2);
    }

    /* Initialize Function handles */
    result = grpc_function_handle_init(handle, host, func_name);
    if (result != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_function_handle_init() error. (%s)\n",
        grpc_error_string(result));
        exit(2);
    }      

    printf("async call start: %s\n", host); 

    /* Asynchronous call */
    result = grpc_call_async(handle, id, n, scalarIn, &scalarOut, arrayIn, arrayOut);
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_call_async() error. (%s)\n",
        grpc_error_string(result));
        exit(2);
    }

    /* Asynchronous call wait*/
    result = grpc_wait_all();
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_wait_all() error. (%s)\n",
        grpc_error_string(result));
        exit(2);
    }

    printf("wait_all end\n");

    /* Destruct Function handles */
    result = grpc_function_handle_destruct(handle);
    if (result != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_function_handle_destruct() error. (%s)\n",
        grpc_error_string(result));
        exit(2);
    }

    printf("destruct handle end\n");

    /* Check Result */
    if(scalarIn != scalarOut){
        printf("Result is NOT correct: scalarIn(%f) != scalarOut(%f)\n", scalarIn, scalarOut);
    }
    else{
        printf("Result is correct: scalarIn(%f) == scalarOut(%f)\n", scalarIn, scalarOut);
    }

    for (i = 0; i < n; i++) {
        if(arrayIn[i] != arrayOut[i]){
            printf("Result is NOT correct: arrayIn(%f) != arrayOut(%f) at %d\n", 
            arrayIn[i], arrayOut[i], i);
            mismatchCount++;
        }
    }
    if(mismatchCount == 0){
        printf("Result is correct for all array args\n");
    }

    /* Finalize */
    result = grpc_finalize();
    if (result != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_finalize() error. (%s)\n",
        grpc_error_string(result));
        exit(2);
    }

    free(host);
    free(handle);
    free(id);

    return 0;
}
Esempio n. 4
0
int
main(int argc, char *argv[])
{
    grpc_function_handle_t *handles;
    grpc_sessionid_t *ids = NULL, id;
    grpc_error_t result;
    char **hosts = NULL;
    double param1[DEF_SCALAR][DEF_SCALAR];
    double param2[DEF_SCALAR][DEF_SCALAR];
    double *param3;
    double wk1, wk2;
    int n;
    int index, base;
    int i, j, k;

    if (argc < 2) {
        fprintf(stderr, "USAGE: %s HOSTNAME.\n", argv[0]);
        fprintf(stderr, "USAGE: %s HOSTNAME1 HOSTNAME2...\n", argv[0]);
        exit(2);
    }

    n = argc - 1;
    hosts = (char **)malloc(sizeof(char *) * n);
    if (hosts == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage for hostname.\n");
        exit(2);
    }
    for (i = 1, j = 0; j < n; i++, j++) {
        hosts[j] = NULL;
        hosts[j] = strdup(argv[i]);
        if (*hosts == NULL) {
            fprintf(stderr,
                "strdup: Can't allocate the storage for hostname.\n");
            exit(2);
        }
    }

    handles = (grpc_function_handle_t *)malloc(
        sizeof(grpc_function_handle_t) * n);
    if (handles == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage for handles.\n");
        exit(2);
    }

    ids = (grpc_sessionid_t *)malloc(sizeof(grpc_sessionid_t) * n);
    if (ids == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage.\n");
        exit(2);
    }

    param3 = (double *)malloc(sizeof(double) * n * DEF_SCALAR * DEF_SCALAR);
    if (param3 == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage.\n");
        exit(2);
    }

    /* Initialize the parameter */
    for (j = 0, wk1 = 1.0, wk2 = 500.0; j < DEF_SCALAR; j++) {
        for (k = 0; k < DEF_SCALAR; k++) {
            param1[j][k] = wk1++;
            param2[j][k] = wk2++;
        }
    }
    for (i = 0; i < (n * DEF_SCALAR * DEF_SCALAR); i++) {
    	param3[i] = 0.0;
    }

    for (i = 0; i < DEF_SCALAR; i++) {
        for (j = 0; j < DEF_SCALAR; j++) {
            fprintf(stdout, "%f ", param1[i][j]);
        }
        fprintf(stdout, "\n");
    }
    fprintf(stdout, "\n");

    for (i = 0; i < DEF_SCALAR; i++) {
        for (j = 0; j < DEF_SCALAR; j++) {
            fprintf(stdout, "%f ", param2[i][j]);
        }
        fprintf(stdout, "\n");
    }
    fprintf(stdout, "\n");

    /* Initialize */
    result = grpc_initialize(config_file);
    if (result != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_initialize() error. (%s)\n",
	    grpc_error_string(result));
        exit(2);
    }

    /* Initialize Function handles */
    for (i = 0; i < n; i++) {
        result = grpc_function_handle_init(&handles[i], hosts[i],
            func_name);
        if (result != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_function_handle_init() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }      
    }
    /* Asynchronous call */
    for (i = 0; i < n; i++) {
        result = grpc_call_async(&handles[i], &ids[i], DEF_SCALAR, param1,
            param2, param3 + (i * DEF_SCALAR * DEF_SCALAR));
        if (result != GRPC_NO_ERROR){
            fprintf(stderr, "grpc_call_async() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }
    }

    for (k = 0; k < n; k++) {
        /* Wait any session */
        result = grpc_wait_any(&id);
        if (result != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_wait_any() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }

        fprintf(stdout, "got Session ID = %d\n", id);
	for (index = 0; index < n; index++) {
            if (ids[index] == id) {
                break;
            }
        }
        fprintf(stdout, "index = %d, ID = %d\n", index, id);

        base = index * DEF_SCALAR * DEF_SCALAR;
	for (i = 0; i < DEF_SCALAR; i++) {
            for (j = 0; j < DEF_SCALAR; j++) {
                fprintf(stdout, "%f ",
                    param3[base + (i * DEF_SCALAR) + j]);
            }
            fprintf(stdout, "\n");
        }
        fprintf(stdout, "\n");
    }

    /* Destruct Function handles */
    for (i = 0; i < n; i++) {
        result = grpc_function_handle_destruct(&handles[i]);
        if (result != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_function_handle_destruct() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }
    }

    /* Finalize */
    result = grpc_finalize();
    if (result != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_finalize() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    for (i = 0; i < n; i++) {
        free(hosts[i]);
    }
    free(hosts);
    free(handles);
    free(ids);
    free(param3);

    return 0;
}
Esempio n. 5
0
int
cancel_all_test()
{
    grpc_sessionid_t id[M];
    grpc_error_t ret;
    int i;
    int nInit = 0;
    int nCall = 0;
    int rVal = 1;

    for (nInit = 0; nInit < M; nInit++) {
        ret = grpc_function_handle_default(&handles[nInit], MODULE_NAME "/cancel");
        if (ret != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_function_handle_default() error. (%s)\n",
                grpc_error_string(ret));
            rVal = 0;
            goto finalize;
        }
    }

    for (nCall = 0; nCall < M; nCall++) {
        ret = grpc_call_async(&handles[nCall], &id[nCall]);
        if (ret != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_call_async() error. (%s)\n",
                grpc_error_string(ret));
            rVal = 0;
            break;
        }
    }

    ret = grpc_cancel_all();
    if (ret != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_cancel() error. (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
    }

    ret = grpc_wait_all();
    if (ret != GRPC_SESSION_FAILED) {
        fprintf(stderr, "grpc_wait_all() error. (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
    }

    for (i = 0; i < nCall; i++) {
        if (grpc_get_error(id[i]) == GRPC_CANCELED_NP) {
            if (verbose) {
                printf("session %d is canceled\n", id[i]);
            }
        } else {
            rVal = 0;
            if (verbose) {
                printf("session %d is not canceled: something went wrong.\n",
                    id[i]);
            }
        }
    }

finalize:
    for (i = 0; i < nInit; i++) {
        ret = grpc_function_handle_destruct(&handles[i]);
        if (ret != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_function_handle_destruct() error. (%s)\n",
                grpc_error_string(ret));
            rVal = 0;
        }
    }

    return rVal;
}
Esempio n. 6
0
int
cancel_test()
{
    grpc_sessionid_t id;
    grpc_error_t ret;
    int rVal = 1;
    int handleInitialized = 0;

    ret = grpc_function_handle_default(&handles[0], MODULE_NAME "/cancel");
    if (ret != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_function_handle_default() error. (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
        goto finalize;
    }
    handleInitialized = 1;

    ret = grpc_call_async(&handles[0], &id);
    if (ret != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_call_async() error. (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
        goto finalize;
    }

    ret = grpc_cancel(id);
    if (ret != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_cancel() error. (%s)\n",
            grpc_error_string(ret));
        rVal = 0;
        goto finalize;
    }

    ret = grpc_wait(id);
    if (ret != GRPC_SESSION_FAILED) {
        fprintf(stderr, "grpc_wait() error. (%s)\n",
            grpc_error_string(ret));
        if (verbose) {
            printf("session %d was finished successfully...\n", id);
        }
        rVal = 0;
        goto finalize;
    }

    if (grpc_get_error(id) == GRPC_CANCELED_NP) {
        if (verbose) {
            printf("session %d is canceled\n", id);
        }
    } else {
        rVal = 0;
        if (verbose) {
            printf("session %d is not canceled: something went wrong.\n", id);
        }
    }

finalize:
    if (handleInitialized != 0) {
        ret = grpc_function_handle_destruct(&handles[0]);
        if (ret != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_function_handle_destruct() error. (%s)\n",
                grpc_error_string(ret));
            rVal = 0;
        }
        handleInitialized = 0;
    }
    return rVal;
}
Esempio n. 7
0
int
main(int argc, char *argv[])
{
    grpc_function_handle_t *handles;
    grpc_sessionid_t *ids = NULL;
    grpc_error_t result = GRPC_NO_ERROR;
    char **hosts = NULL;
    double pi;
    long *count = NULL;
    long times, sum;
    int n;
    int i, j;

    if (argc < 3){
        fprintf(stderr, "USAGE: %s TIMES HOSTNAME\n", argv[0]);
        fprintf(stderr, "USAGE: %s TIMES HOSTNAME1 HOSTANME2...\n", argv[0]);
        exit(2);
    }

    n = argc - 2;
    hosts = (char **)malloc(sizeof(char *) * n);
    if (hosts == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage for hostname.\n");
        exit(2);
    }
    for (i = 2, j = 0; j < n; i++, j++) {
        hosts[j] = NULL;
        hosts[j] = strdup(argv[i]);
        if (*hosts == NULL) {
            fprintf(stderr,
                 "strdup: Can't allocate the storage for hostname.\n");
            exit(2);
        }
    }

    times = atol(argv[1]) / n;

    handles = (grpc_function_handle_t *)malloc(
        sizeof(grpc_function_handle_t) * n);
    if (handles == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage for handles.\n");
        exit(2);
    }

    count = (long *)malloc(sizeof(long) * n);
    if (count == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage.\n");
        exit(2);
    }

    ids = (grpc_sessionid_t *)malloc(sizeof(grpc_sessionid_t) * n);
    if (ids == NULL) {
        fprintf(stderr, "malloc: Can't allocate the storage.\n");
        exit(2);
    }

    /* Initialize */
    result = grpc_initialize(config_file);
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_initialize() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    /* Initialize Function handles */
    for (i = 0; i < n; i++) {
        result = grpc_function_handle_init(&handles[i], hosts[i],
            func_name);
        if (result != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_function_handle_init() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }      
    }

    /* Asynchronous call */
    for (i = 0; i < n; i++) {
        result = grpc_call_async(&handles[i], &ids[i], i, times, &count[i]);
        if (result != GRPC_NO_ERROR){
            fprintf(stderr, "grpc_call_async() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }
    }

    /* Asynchronous call */
    result = grpc_wait_all();
    if (result != GRPC_NO_ERROR){
        fprintf(stderr, "grpc_wait_all() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    /* Destruct Function handles */
    for (i = 0; i < n; i++) {
        result = grpc_function_handle_destruct(&handles[i]);
        if (result != GRPC_NO_ERROR) {
            fprintf(stderr, "grpc_function_handle_destruct() error. (%s)\n",
                grpc_error_string(result));
            exit(2);
        }
    }

    /* Compute and display pi. */
    for (i = 0, sum = 0; i < n; i++) {
        sum += count[i];
    }
    pi = 4.0 * (sum / ((double) times * n));
    printf("PI = %f\n", pi);

    /* Finalize */
    result = grpc_finalize();
    if (result != GRPC_NO_ERROR) {
        fprintf(stderr, "grpc_finalize() error. (%s)\n",
            grpc_error_string(result));
        exit(2);
    }

    for (i = 0; i < n; i++) {
        free(hosts[i]);
    }
    free(hosts);
    free(handles);
    free(count);
    free(ids);

    return 0;
}