int main(int argc, char **argv) { int k; int N; int i; int rank; int max_rank; int workload; int ret = 0; int bcaster = 0; double sec; double *A = NULL; double **A2D = NULL; double *Ap = NULL; double **Ap2D = NULL; double *Ak = NULL; FILE *fp = NULL; void (*propagate) (void*, int, MPI_Datatype, int, MPI_Comm); time_struct ts; usage(argc, argv); propagate = get_propagation(argc, argv); MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &max_rank); /* Root gets the matrix */ if (rank == 0){ Matrix *mat = get_matrix(argv[1], max_rank, CONTINUOUS); N = mat->N; A = mat->A; A2D = appoint_2D(A, N, N); } /* And broadcasts N */ MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD); workload = (N / max_rank) + 1; // Max number of rows for each thread #if main_DEBUG if (rank == 0){ debug("A: \n"); print_matrix_2d(N, N, A); } #endif /* Allocations */ Ak = malloc(N * sizeof(double)); Ap = malloc(workload * N * sizeof(double)); /* Scatter the table to each thread's Ap */ MPI_Scatter(A, workload * N, MPI_DOUBLE, \ Ap, workload * N, MPI_DOUBLE, 0, MPI_COMM_WORLD); Ap2D = appoint_2D(Ap, workload, N); /* Init Communication Timer */ time_struct_init(&ts); /* Start Total Timer */ MPI_Barrier(MPI_COMM_WORLD); if(rank == 0) { sec = timer(); } for (k = 0; k < N - 1 ; k++) { /* Find who owns the k-th row */ bcaster = k / workload; /* The broadcaster puts his k-th row in the Ak buffer */ if (rank == bcaster) { i = k % workload; memcpy(&Ak[k], &Ap2D[i][k], (N-k) * sizeof(double)); } /* Everyone receives the k-th row */ time_struct_set_timestamp(&ts); (*propagate) (&Ak[k], N-k, MPI_DOUBLE, bcaster, MPI_COMM_WORLD); time_struct_add_timestamp(&ts); /* And off you go to work. */ process_rows(k, rank, N, workload, Ap2D, Ak); } /* Stop Timing */ MPI_Barrier(MPI_COMM_WORLD); if(rank == 0) { sec = timer(); printf("Calc Time: %lf\n", sec); } printf("Rank: %d Comm Time: %lf\n", rank, get_seconds(&ts)); /* Gather the table from each thread's Ap */ MPI_Gather(Ap, workload * N, MPI_DOUBLE, \ A, workload * N, MPI_DOUBLE, 0, MPI_COMM_WORLD); ret = MPI_Finalize(); if(ret == 0) { debug("%d FINALIZED!!! with code: %d\n", rank, ret); } else { debug("%d NOT FINALIZED!!! with code: %d\n", rank, ret); } /* Format and output solved matrix */ if(rank == 0) { upper_triangularize(N, A2D); fp = fopen(argv[2], "w"); fprint_matrix_2d(fp, N, N, A); fclose(fp); free(A); free(A2D); } free(Ap); free(Ap2D); free(Ak); return 0; }
int main(int argc, char **argv) { if(argc < 5) { fprintf(stderr, "stream_producer needs at least 4 arguments\n"); exit(1); } if(strcmp(argv[1], "--write-fifo") != 0) { fprintf(stderr, "stream_producer first arg must be --write-fifo\n"); exit(1); } if(strcmp(argv[3], "--read-fifo") != 0) { fprintf(stderr, "stream producer third arg must be --read-fifo\n"); exit(1); } printf("C binopt: start\n"); ciel_init(argv[2], argv[4]); json_t* task_private = ciel_get_task(); json_error_t error_bucket; json_t* kwargs; char* s_str; char* k_str; char* t_str; char* v_str; char* rf_str; char* cp_str; char* n_str; char* chunk_str; char* start_str; fprintf(stderr, "doing decode\n"); if(json_unpack_ex(task_private, &error_bucket, 0, "{s[sssssssss]so}", "proc_pargs", &s_str, &k_str, &t_str, &v_str, &rf_str, &cp_str, &n_str, &chunk_str, &start_str, "proc_kwargs", &kwargs)) { ciel_json_error("Failed to decode proc_pargs", &error_bucket); exit(1); } fprintf(stderr, "doing conversion\n"); s = strtod(s_str, NULL); k = strtod(k_str, NULL); t = strtod(t_str, NULL); v = strtod(v_str, NULL); rf = strtod(rf_str, NULL); cp = strtod(cp_str, NULL); n = (int)strtod(n_str, NULL); chunk = (int)strtod(chunk_str, NULL); start = strcmp(start_str, "false"); fprintf(stderr, "Got arguments: %g %g %g %g %g %g %d %d %s\n", s, k, t, v, rf, cp, n, chunk, start_str); if(!start) { json_t* in_ref = json_object_get(kwargs, "input_ref"); binopt_ciel_in = ciel_open_ref_async(in_ref, 1, 1, 0); ciel_set_input_unbuffered(binopt_ciel_in); } else { binopt_ciel_in = 0; } json_decref(task_private); binopt_ciel_out = ciel_open_output(0, 1, 0, 0); ciel_set_output_unbuffered(binopt_ciel_out); init_vals(); if (start == 1) { OUTPUT(n); gen_initial_optvals(); } else { int rowstart=0; INPUT(rowstart); if (rowstart==0) { double v; INPUT(v); char buf[20]; int chars_written = min(snprintf(buf, 20, "%.9f\n", v), 20); ciel_write_output(binopt_ciel_out, buf, chars_written); } else { int rowto = (rowstart - chunk) > 0 ? (rowstart - chunk) : 0; process_rows(rowstart, rowto); } } if(!start) { ciel_close_ref(binopt_ciel_in); } ciel_close_output(binopt_ciel_out); ciel_exit(); return 0; }