/* Função que realiza a leitura da matriz, determina os valores máximos de cada * coluna e determina se os dados passados pelo usuário são válidos */ void read_mat(int matrix[MAX_N][MAX_N], int n, int col_max[MAX_N]) { int i, j, verify; for (j = 0; j < n; j++) { verify = scanf("%d", &matrix[0][j]); if (verify != 1) invalid_input(); /* Contabiliza os valores da primeira linha para comparação */ col_max[j] = matrix[0][j]; } exclude_com(); for (i = 1; i < n; i++) { for (j = 0; j < n; j++) { verify = scanf("%d", &matrix[i][j]); if (verify != 1) invalid_input(); /* Faz as comparações para encontrar o valor máximo de cada coluna*/ if (matrix[i][j] > col_max[j]) col_max[j] = matrix[i][j]; } exclude_com(); } }
gfarm_error_t parse_string_long(char **linep, int lineno, const char *diag, long *retvalp) { char *line = *linep, *s; int len; long retval; line += strspn(line, space); /* skip space */ len = strcspn(line, space); if (len == 0 || line[len] == '\0') return (invalid_input(lineno)); line[len] = '\0'; errno = 0; retval = strtol(line, &s, 0); if (s == line) { return (invalid_input(lineno)); } else if (*s != '\0') { fprintf(stderr, "line %d: garbage \"%s\" in %s \"%s\"\n", lineno, s, diag, line); return (GFARM_ERR_INVALID_ARGUMENT); } else if (errno != 0 && (retval == LONG_MIN || retval == LONG_MAX)) { fprintf(stderr, "line %d: %s on \"%s\"\n", lineno, strerror(errno), line); return (GFARM_ERR_INVALID_ARGUMENT); } line += len + 1; *linep = line; *retvalp = retval; return (GFARM_ERR_NO_ERROR); }
/*------------------------------------------------------*/ int getAndMoveToFrontDecode(unsigned limit) { char yy[256]; int32_t i, tmpOrigPtr; tmpOrigPtr = getUInt32(); origPtr = (tmpOrigPtr < 0 ? -tmpOrigPtr : tmpOrigPtr) - 1; initModels(); for (i = 0; i < 256; i++) yy[i] = i; for (block_end = 0; ; block_end++) { unsigned nextSym; nextSym = getSymbol(&models[MODEL_BASIS]); if (nextSym == VAL_RUNA || nextSym == VAL_RUNB) { /* Acquire run-length bits, most significant first */ unsigned n; n = 0; do { n <<= 1; n++; if (nextSym == VAL_RUNA) n++; nextSym = getSymbol(&models[MODEL_BASIS]); } while (nextSym == VAL_RUNA || nextSym == VAL_RUNB); if (block_end + n > limit) invalid_input("file corrupt"); memset(&ll[block_end], yy[0], n); block_end += n; } /* if */ if (nextSym == VAL_EOB) break; nextSym = getMTFVal(nextSym); assert(INRANGE(nextSym, 1, 255)); if (block_end >= limit) invalid_input("file corrupt"); ll[block_end] = yy[nextSym]; memmove(&yy[1], &yy[0], nextSym); yy[0] = ll[block_end]; } /* for */ return tmpOrigPtr < 0; } /* getAndMoveToFrontDecode */
/* Interface functions */ void decompress(void) { int finish; unsigned blocksize; blocksize = read_magic() * 100000; lc_recallocp(&block, blocksize * sizeof(*block)); lc_recallocp(&ll, blocksize * sizeof(*ll)); lc_recallocp(&zptr, blocksize * sizeof(*zptr)); initBogusModel(); arithCodeStartDecoding(); do { finish = getAndMoveToFrontDecode(blocksize); undoReversibleTransformation(); spotBlock(); unRLEandDump(finish); } while (!finish); if (~getUInt32() != output_bs.crc) invalid_input("CRC error"); } /* decompress */
void unRLEandDump(int finish) { int chPrev; unsigned i, count; if (finish) block_end--; count = 0; chPrev = -1; for (i = 0; i < block_end; i++) { int ch; ch = block[i]; bs_put_byte(ch); if (ch != chPrev) { chPrev = ch; count = 1; continue; } count++; if (count < 4) continue; i++; if (i >= block_end) invalid_input("file corrupt"); for (count = block[i]; count > 0; count--) bs_put_byte(ch); } /* for */ if (finish && block[i] != 42) invalid_input("file corrupt"); } /* unRLEandDump */
int main() { int n, verify; int matrix[MAX_N][MAX_N]; int col_max[MAX_N]; verify = scanf("%d", &n); if ((verify != 1) || (n > 20) || (n < 1)) invalid_input(); exclude_com(); read_mat(matrix, n, col_max); printf("Os pontos de sela da matriz são:\n\n"); check_selas(matrix, col_max, n); return 0; }
unsigned read_magic(void) { char magic[4]; unsigned i, o; for (i = 0; i < MEMBS_OF(magic); i++) { magic[i] = 0; for (o = 8; o > 0; o--) { magic[i] <<= 1; magic[i] |= bs_get_bit(); } } if (magic[0] != 'B' || magic[1] != 'Z' || magic[2] != '0' || magic[3] < '1') invalid_input("invalid magic"); return magic[3] - '0'; } /* read_magic */
int main(int argc, char *argv[]) { struct timespec start, finish; clock_t start_t, end_t, total_t; double elapsed; clock_gettime(CLOCK_MONOTONIC, &start); start_t = clock(); int print, test, n, benchmark, help, failed; Matrix M, Msave = 0; // Cilk_time tm_begin, tm_elapsed; // Cilk_time wk_begin, wk_elapsed; // Cilk_time cp_begin, cp_elapsed; n = DEFAULT_SIZE; print = 0; test = 0; /* Parse arguments. */ get_options(argc, argv, specifiers, opt_types, &n, &print, &test, &benchmark, &help); if (help) return usage(); if (benchmark) { switch (benchmark) { case 1: /* short benchmark options -- a little work */ n = 16; break; case 2: /* standard benchmark options */ n = DEFAULT_SIZE; break; case 3: /* long benchmark options -- a lot of work */ n = 2048; break; } } if (invalid_input(n)) return 1; nBlocks = n / BLOCK_SIZE; /* Allocate matrix. */ M = (Matrix) malloc(n * n * sizeof(double)); if (!M) { fprintf(stderr, "Allocation failed.\n"); return 1; } /* Initialize matrix. */ init_matrix(M, nBlocks); if (print) print_matrix(M, nBlocks); if (test) { Msave = (Matrix) malloc(n * n * sizeof(double)); if (!Msave) { fprintf(stderr, "Allocation failed.\n"); return 1; } memcpy((void *) Msave, (void *) M, n * n * sizeof(double)); } /* Timing. "Start" timers */ // sync; // cp_begin = Cilk_user_critical_path; // wk_begin = Cilk_user_work; // tm_begin = Cilk_get_wall_time(); lu(M, nBlocks); // sync; /* Timing. "Stop" timers */ // tm_elapsed = Cilk_get_wall_time() - tm_begin; // wk_elapsed = Cilk_user_work - wk_begin; // cp_elapsed = Cilk_user_critical_path - cp_begin; /* Test result. */ if (print) print_matrix(M, nBlocks); failed = ((test) && (!(test_result(M, Msave, nBlocks)))); if (failed) printf("WRONG ANSWER!\n"); else { // printf("\nCilk Example: lu\n"); // printf(" running on %d processor%s\n\n", // Cilk_active_size, Cilk_active_size > 1 ? "s" : ""); // printf("Options: (n x n matrix) n = %d\n\n", n); // printf("Running time = %4f s\n", // Cilk_wall_time_to_sec(tm_elapsed)); // printf("Work = %4f s\n", Cilk_time_to_sec(wk_elapsed)); // printf("Critical path = %4f s\n\n", // Cilk_time_to_sec(cp_elapsed)); printf("TRUE!\n"); } /* Free matrix. */ free(M); if (test) free(Msave); end_t = clock(); clock_gettime(CLOCK_MONOTONIC, &finish); elapsed = (finish.tv_sec - start.tv_sec); elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; printf("[ELAPSE] time by CPU: %f\n", elapsed); printf("Total time taken by CPU: %d\n", end_t - start_t); return 0; }
gfarm_error_t add_line(char *line, int lineno) { gfarm_error_t e; long port, ncpu, flags; int len, nhostaliases; char *s, *hostname, *architecture; char *hostaliases[MAX_HOSTALIASES + 1]; /* parse architecture */ line += strspn(line, space); /* skip space */ len = strcspn(line, space); if (len == 0 || line[len] == '\0') return (invalid_input(lineno)); line[len] = '\0'; architecture = line; line += len + 1; s = validate_architecture(architecture); if (s != NULL) { fprintf(stderr, "line %d: invalid character '%c' in architecture \"%s\"\n", lineno, *s, architecture); return (GFARM_ERR_INVALID_ARGUMENT); } e = parse_string_long(&line, lineno, "ncpu", &ncpu); if (e != GFARM_ERR_NO_ERROR) return (e); /* parse hostname */ line += strspn(line, space); /* skip space */ len = strcspn(line, space); if (len == 0) return (invalid_input(lineno)); hostname = line; if (line[len] == '\0') { line += len; } else { line[len] = '\0'; line += len + 1; } s = validate_hostname(hostname); if (s != NULL) { fprintf(stderr, "line %d: invalid character '%c' in hostname \"%s\"\n", lineno, *s, hostname); return (GFARM_ERR_INVALID_ARGUMENT); } e = parse_string_long(&line, lineno, "port", &port); if (e != GFARM_ERR_NO_ERROR) return (e); e = parse_string_long(&line, lineno, "flags", &flags); if (e != GFARM_ERR_NO_ERROR) return (e); /* parse hostaliases */ for (nhostaliases = 0;; nhostaliases++) { line += strspn(line, space); /* skip space */ if (*line == '\0') break; len = strcspn(line, space); /* assert(len > 0); */ if (nhostaliases >= MAX_HOSTALIASES) { fprintf(stderr, "line %d: " "number of hostaliases exceeds %d\n", lineno, nhostaliases); return (GFARM_ERR_INVALID_ARGUMENT); } hostaliases[nhostaliases] = line; if (line[len] == '\0') { line += len; } else { line[len] = '\0'; line += len + 1; } s = validate_hostname(hostaliases[nhostaliases]); if (s != NULL) { fprintf(stderr, "line %d: " "invalid character '%c' in hostalias \"%s\"\n", lineno, *s, hostaliases[nhostaliases]); return (GFARM_ERR_INVALID_ARGUMENT); } } hostaliases[nhostaliases] = NULL; e = add_host(hostname, port, hostaliases, architecture, ncpu, flags); if (e != GFARM_ERR_NO_ERROR) fprintf(stderr, "line %d: %s\n", lineno, gfarm_error_string(e)); return (e); }
int main(int argc, char *argv[]) { struct timespec start, finish; double elapsed; clock_gettime(CLOCK_MONOTONIC, &start); int print, test, n, benchmark, help, failed; Matrix M, Msave = 0; n = DEFAULT_SIZE; print = 0; test = 0; /* Parse arguments. */ get_options(argc, argv, specifiers, opt_types, &n, &print, &test, &benchmark, &help); if (help) return usage(); if (benchmark) { switch (benchmark) { case 1: /* short benchmark options -- a little work */ n = 16; break; case 2: /* standard benchmark options */ n = DEFAULT_SIZE; break; case 3: /* long benchmark options -- a lot of work */ n = 2048; break; } } if (invalid_input(n)) return 1; nBlocks = n / BLOCK_SIZE; /* Allocate matrix. */ M = (Matrix) malloc(n * n * sizeof(double)); if (!M) { fprintf(stderr, "Allocation failed.\n"); return 1; } /* Initialize matrix. */ init_matrix(M, nBlocks); if (print) print_matrix(M, nBlocks); if (test) { Msave = (Matrix) malloc(n * n * sizeof(double)); if (!Msave) { fprintf(stderr, "Allocation failed.\n"); return 1; } memcpy((void *) Msave, (void *) M, n * n * sizeof(double)); } /* Timing. "Start" timers */ cilk_sync; cilk_spawn lu(M, nBlocks); cilk_sync; /* Test result. */ if (print) print_matrix(M, nBlocks); failed = ((test) && (!(test_result(M, Msave, nBlocks)))); if (failed) printf("WRONG ANSWER!\n"); else { } /* Free matrix. */ free(M); clock_gettime(CLOCK_MONOTONIC, &finish); elapsed = (finish.tv_sec - start.tv_sec); elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; printf("Total time taken by CPU: %f\n", elapsed); if (test) free(Msave); return 0; }
int main(int argc, char *argv[]) { int print, test, n, benchmark, help, failed; Matrix M, Msave = 0; pp_time_t tm; memset( &tm, 0, sizeof(tm) ); n = DEFAULT_SIZE; print = 0; test = 0; /* Parse arguments. */ get_options(argc, argv, specifiers, opt_types, &n, &print, &test, &benchmark, &help); if (help) return usage(); if (benchmark) { switch (benchmark) { case 1: /* short benchmark options -- a little work */ n = 16; break; case 2: /* standard benchmark options */ n = DEFAULT_SIZE; break; case 3: /* long benchmark options -- a lot of work */ n = 2048; break; } } if (invalid_input(n)) return 1; nBlocks = n / BLOCK_SIZE; /* Allocate matrix. */ M = (Matrix) malloc(n * n * sizeof(double)); if (!M) { fprintf(stderr, "Allocation failed.\n"); return 1; } /* Initialize matrix. */ init_matrix(M, nBlocks); if (print) print_matrix(M, nBlocks); if (test) { Msave = (Matrix) malloc(n * n * sizeof(double)); if (!Msave) { fprintf(stderr, "Allocation failed.\n"); return 1; } memcpy((void *) Msave, (void *) M, n * n * sizeof(double)); } /* Timing. "Start" timers */ pp_time_start( &tm ); lu(M, nBlocks); /* Timing. "Stop" timers */ pp_time_end( &tm ); /* Test result. */ if (print) print_matrix(M, nBlocks); failed = ((test) && (!(test_result(M, Msave, nBlocks)))); if (failed) printf("WRONG ANSWER!\n"); else { printf("\nCilk Example: lu\n"); printf("Options: (n x n matrix) n = %d\n\n", n); printf("Running time = %g %s\n", pp_time_read(&tm), pp_time_unit() ); } /* Free matrix. */ free(M); if (test) free(Msave); return 0; }