void read_image (char *filename) { FILE *fp; int i, j, mx; char str[1024]; int64_t *img, v0; if ((fp = fopen (filename, "r")) == NULL) { fprintf (stderr, "couldn't open image file\n"); exit (1); } fscanf (fp, "%s", str); if (strcmp ("P2", str) != 0) { fprintf (stderr, "image format error; must be a 'P2' pgm file\n"); exit (1); } fscanf (fp, "%d %d", &SM, &SN); fscanf (fp, "%d", &mx); printf ("image size is %dx%d\n", SN, SM); SZ = SN*SM*8; if (SZ & 0x1f) SZ = (SZ+32) & 0xffffffe0; A = (int64_t*) Cache_Aligned_Allocate (SZ); B = (int64_t*) Cache_Aligned_Allocate (SZ); for (i=0; i<SN; i++) for (j=0; j<SM; j++) { fscanf (fp, "%lld", &v0); REF(A,i,j) = v0; } }
int main (int argc, char *argv[]) { FILE *res_map, *res_cpu; int i, num, nret; int64_t *A, *B; int64_t tm; int mapnum = 0; if ((res_map = fopen ("res_map", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_map'\n"); exit (1); } if ((res_cpu = fopen ("res_cpu", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_cpu'\n"); exit (1); } if (argc < 2) { fprintf (stderr, "need number of elements (up to %d) as arg\n", SZ); exit (1); } if (sscanf (argv[1], "%d", &num) < 1) { fprintf (stderr, "need number of elements (up to %d) as arg\n", SZ); exit (1); } if (num > SZ) { fprintf (stderr, "need number of elements (up to %d) as arg\n", SZ); exit (1); } A = (int64_t*) Cache_Aligned_Allocate (SZ * sizeof (int64_t)); B = (int64_t*) Cache_Aligned_Allocate (SZ * sizeof (int64_t)); srandom (99); for (i=0; i<SZ; i++) { A[2*i] = random () & 0xffff; A[2*i+1] = A[2*i] + 100000; } map_allocate (1); // call the MAP routine subr (A, B, num, &nret, &tm, mapnum); printf ("combined DMA and compute time: %lld clocks\n", tm); for (i=0; i<nret; i++) fprintf (res_map, "%lld\n", B[i]); for (i=0; i<num; i++) if (A[i] > 30000) fprintf (res_cpu, "%lld\n", A[i]*17); map_free (1); exit(0); }
int main (int argc, char *argv[]) { FILE *res_map, *res_cpu; int i, num; int64_t *A0, *A1, *B; int64_t tm; int mapnum = 0; if ((res_map = fopen ("res_map", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_map'\n"); exit (1); } if ((res_cpu = fopen ("res_cpu", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_cpu'\n"); exit (1); } if (argc < 2) { fprintf (stderr, "need number of elements as arg\n"); exit (1); } if (sscanf (argv[1], "%d", &num) < 1) { fprintf (stderr, "need number of elements as arg\n"); exit (1); } if ((num < 1) || (num > MAX_OBM_SIZE)) { fprintf (stderr, "number of elements must be in the range 1 through %d\n", MAX_OBM_SIZE); exit (1); } A0 = (int64_t*) Cache_Aligned_Allocate (num * sizeof (int64_t)); A1 = (int64_t*) Cache_Aligned_Allocate (num * sizeof (int64_t)); B = (int64_t*) Cache_Aligned_Allocate (num * sizeof (int64_t)); srandom (99); for (i=0; i<num; i++) { A0[i] = random (); A1[i] = random (); } map_allocate (1); subr (A0, A1, B, num, &tm, mapnum); printf ("%lld clocks\n", tm); for (i=0; i<num; i++) { fprintf (res_map, "%lld\n", B[i]); fprintf (res_cpu, "%lld\n", A0[i]+A1[i]); } map_free (1); exit(0); }
void cpu_compute (char *name) { int i, j, mx; char str[1024]; int64_t *img, v0; int64_t *B_correct; uint8_t V[9], rr; B_correct = (int64_t*) Cache_Aligned_Allocate (SZ); for (i=0; i<SN-(WN-1); i++) for (j=0; j<SM-(WM-1); j++) { V[0] = REF(A,i,j); V[1] = REF(A,i,j+1); V[2] = REF(A,i,j+2); V[3] = REF(A,i+1,j); V[4] = REF(A,i+1,j+1); V[5] = REF(A,i+1,j+2); V[6] = REF(A,i+2,j); V[7] = REF(A,i+2,j+1); V[8] = REF(A,i+2,j+2); rr = cpu_median (V); REF(B_correct,i,j) = rr; } dump_image (B_correct, name); }
// Gets start and end points in dictionary for a certain wordlength // Gets a pointer to a new dictionary with only the specified wordlength // Creates new int64_t* array containing ciphertext for subroutine // Decrypts using map, which returns the decryption key // Prints the key and the plaintext // Frees all malloc'd resources created in block int64_t* executeSubroutine(char** words, char* ciphertextchars, int ciphertextlength, int wordlength, int keylength, int64_t* tm, int mapnum){ int start=0, end=0, wordcount=0, i=0; // Get a new short dictionary int64_t** wordlengthOnly = getShortDictionary(words, wordlength); int64_t* foundkey = Cache_Aligned_Allocate(keylength * sizeof(int64_t)); start = getStart(words, wordlength); end = getEnd(words, wordlength); wordcount = end - start + 1; int64_t* ciphertext = malloc(sizeof(int64_t) * (ciphertextlength+1)); // Extra for null char for(i = 0; i < ciphertextlength; i++){ ciphertext[i] = (int64_t)ciphertextchars[i]; } // Decrypt on MAP subr (&wordlengthOnly[0][0], ciphertext, ciphertextlength, foundkey, wordcount, wordlength, keylength, &tm, mapnum); char *key = malloc(sizeof(char) * (keylength+1)); key[keylength] = '\0'; printf("MAP subroutine found key '"); for(i = 0; i < keylength; i++){ printf("%c", (char)(foundkey[i]) ); key[i] = (char)foundkey[i]; } printf("' and plaintext %s \n", decrypt( ciphertextchars, ciphertextlength, key, keylength )); printf ("MAP completed in %lld clocks.\n", tm); free(foundkey); freeShortDictionary(wordlengthOnly, wordcount); free(ciphertext); free(key); return tm; }
int main (int argc, char *argv[]) { FILE *res_map, *res_cpu; int i,j; /* * Text memory routes to Bank A and it is the input to Map Processor * ### Currently limited to 100 words - Bank A * Core Dump is the output from MAP processer and it contains the * architecture state in the form of register file data * ### Fixed at 32 words - Bank B * Data memory is also the output from MAP processor and it contains * the final state of processor's memory * ### Currently limited to 100 words - Bank D */ int64_t *text_memory, *core_dump, *data_memory; int64_t tm, accum=0; int mapnum = 0; /* Statically initialize PC to the start of text memory */ int64_t pc_value = MEM_TEXT_START; /* Program File Variable */ FILE *program; int instruction_word; int instruction_count; int64_t instructions_executed; /* Preliminary Error Checking */ if (argc < 2) { /* Dislay correct usage information */ printf ("[Error] usage: %s <program_file>\n", argv[0]); exit (1); } /* Open the program file for reading */ program = fopen (argv[1], "r"); /* Check for the validity of program file */ if (program == NULL) { printf ("[Error] Can't open program file %s\n", argv[1]); exit (1); } /* Initialize the memories on co-processor */ text_memory = (int64_t*) Cache_Aligned_Allocate ((MEM_TEXT_SIZE) * sizeof (int64_t)); core_dump = (int64_t*) Cache_Aligned_Allocate ((MIPS_ARCH_REGS) * sizeof (int64_t)); data_memory = (int64_t*) Cache_Aligned_Allocate ((MEM_DATA_SIZE) * sizeof (int64_t)); /* Read the program file */ instruction_count = 0; while (fscanf (program, "%x\n", &instruction_word) != EOF) { text_memory[instruction_count] = instruction_word; instruction_count++; } /* Close the program file */ fclose (program); printf ("MIPS Simulator\n\n"); printf ("Read %d words from program file into instruction memory.\n", instruction_count); /* Initialize the remaining instructions as zeros */ for (i = instruction_count; i < (MEM_TEXT_SIZE); i++) { text_memory[i] = 0; } /* Open the files to print data from mips processor */ if ((res_map = fopen ("res_map", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_map'\n"); exit (1); } if ((res_cpu = fopen ("res_cpu", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_cpu'\n"); exit (1); } /* Run the program on simulator */ simulate(argv[1]); /* Dump the simulation result to res_cpu file */ fprintf (res_cpu, "Program : %s\n", argv[1]); fprintf (res_cpu, "Clocks : 0\n"); rdump (res_cpu); mdump (res_cpu); /* Execute the program on MAP processor */ map_allocate (1); subr (text_memory, core_dump, data_memory, &pc_value, &instructions_executed, &tm, mapnum); /* Free the map processor */ map_free (1); /* Print execution results to output files */ fprintf (res_map, "Program : %s\n", argv[1]); fprintf (res_map, "Clocks : %lld\n", tm); fprintf (res_map, "Instruction Count : %d\n", (int)instructions_executed); fprintf (res_map, "Final PC Value : 0x%08x\n", (int)pc_value); rdump_map(res_map, core_dump); mdump_map(res_map, data_memory); /* Close result files */ fclose (res_cpu); fclose (res_map); exit(0); }
int main (int argc, char *argv[]) { FILE *res_map, *res_cpu; int i, num; int64_t *A, *B, *C; int64_t tm0, tm1, tm2; int mapnum = 0; int snapmem = 0; if ((res_map = fopen ("res_map", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_map'\n"); exit (1); } if ((res_cpu = fopen ("res_cpu", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_cpu'\n"); exit (1); } if (argc < 2) { fprintf (stderr, "need number of elements as arg\n"); exit (1); } if (sscanf (argv[1], "%d", &num) < 1) { fprintf (stderr, "need number of elements as arg\n"); exit (1); } if ((num < 1) || (num > MAX_OBM_SIZE)) { fprintf (stderr, "number of elements must be in the range 1 through " "%d\n", MAX_OBM_SIZE); exit (1); } if (argc == 3) { sscanf (argv[2], "%d", &snapmem); printf ("snapmem %i\n",snapmem); } A = (int64_t*) Cache_Aligned_Allocate (num * sizeof (int64_t)); B = (int64_t*) Cache_Aligned_Allocate (num * sizeof (int64_t)); C = (int64_t*) Cache_Aligned_Allocate (num * sizeof (int64_t)); memset (A, 0, num * sizeof (int64_t)); memset (B, 0, num * sizeof (int64_t)); memset (C, 0, num * sizeof (int64_t)); srandom (99); for (i=0; i<num; i++) { A[i] = random (); B[i] = random (); } map_allocate (1); subr (A, B, C, num, &tm0, &tm1, &tm2, mapnum); printf ("DMA to MAP: %8lld clocks\n", tm0); printf ("compute: %8lld clocks\n", tm1); printf ("DMA to CPU: %8lld clocks\n", tm2); for (i=0; i<num; i++) { int64_t v; fprintf (res_map, "%lld\n", C[i]); v = A[i] + B[i]; if (A[i] > B[i]) v = v + 42; fprintf (res_cpu, "%lld\n", v*3); } map_free (1); exit(0); }
int main (int argc, char *argv[]) { FILE *res_map, *res_cpu; int i, idx, num, nvals; int64_t *A, *B, *MB; int64_t tm, res, accum=0; int mapnum = 0; if ((res_map = fopen ("res_map", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_map'\n"); exit (1); } if ((res_cpu = fopen ("res_cpu", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_cpu'\n"); exit (1); } if (argc < 2) { fprintf (stderr, "need number of elements as arg\n"); exit (1); } if (sscanf (argv[1], "%d", &num) < 1) { fprintf (stderr, "need number of elements as arg\n"); exit (1); } if ((num < 1) || (num > MAX_OBM_SIZE)) { fprintf (stderr, "number of elements must be in the range 1 through %d\n", MAX_OBM_SIZE); exit (1); } A = (int64_t*) Cache_Aligned_Allocate (num * sizeof (int64_t)); B = (int64_t*) Cache_Aligned_Allocate (num * sizeof (int64_t)); MB = (int64_t*) Cache_Aligned_Allocate (num * sizeof (int64_t)); srandom (99); idx = 0; for (i=0; i<num; i++) { A[i] = random () & 0xff; if (A[i] < 128) MB[idx++] = A[i]; } for (i=0; i<idx; i++) fprintf (res_cpu, "%lld\n", MB[i]); map_allocate (1); subr (A, B, num, &nvals, &tm, mapnum); printf ("%lld clocks\n", tm); for (i=0; i<nvals; i++) fprintf (res_map, "%lld\n", B[i]); map_free (1); exit(0); }
int main (int argc, char *argv[]) { int i = 0; int j = 0; int64_t* time; if (argc < 2) { fprintf (stderr, "need image file source as arg\n"); exit (1); } /*** Insert Your Code Here ***/ read_image( argv[1] ); int64_t *a; A = (int*) Cache_Aligned_Allocate(n_bytes * sizeof(int)); B = (int*) Cache_Aligned_Allocate(n_bytes * sizeof(int)); // Move image from 2d array into 1d int array (easier to pass to map) int index = 0; for(i = 0; i < n_rows; i++){ for(j = 0; j < n_cols; j++){ A[index] = image[i][j]; index++; } } // prints 289722, which is n_cols*n_rows, and all the values of A, which shows it storing correctly /* int count = 0; for(i = 0; i < n_bytes; i++){ count++; printf("%d\n ", A[i]); } printf("%d\n", count); */ status = map_allocate(nmaps); if(status != 0 ){ fprintf (stderr, "Could not allocate nmaps\n"); exit(2); } int mapnum = nmaps - 1; printf("Calling map subroutine.\n"); subr(A, B, n_rows, n_cols, n_bytes, &time, mapnum); printf("Completed map subroutine.\n"); // Producing the wrong output for some reason. Lot of 0's. /* int count = 0; for(i = 0; i < n_bytes; i++){ count++; printf("%d\n ", B[i]); } printf("%d\n", count); */ // Move from 1d array (B) back to 2d array so it can be printed to file index = 0; for(i = 0; i < n_rows; i++){ for(j = 0; j < n_cols; j++){ image[i][j] = B[index]; index++; } } map_free(nmaps); /*****************************/ //printf ("%lld clocks\n", &time); printf ("%lld clocks\n", time); write_image( 0, "output.pgm" ); exit (0); }
int main (int argc, char *argv[]) { FILE *res_map, *res_cpu; int i, num; int64_t *A, *B, *C, *D; int64_t tm; int mapnum = 0; if ((res_map = fopen ("res_map", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_map'\n"); exit (1); } if ((res_cpu = fopen ("res_cpu", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_cpu'\n"); exit (1); } if (argc < 2) { fprintf (stderr, "need number of elements (up to %d) as arg\n", SZ); exit (1); } if (sscanf (argv[1], "%d", &num) < 1) { fprintf (stderr, "need number of elements (up to %d) as arg\n", SZ); exit (1); } if (num > SZ) { fprintf (stderr, "need number of elements (up to %d) as arg\n", SZ); exit (1); } A = (int64_t*) Cache_Aligned_Allocate (SZ * sizeof (int64_t)); B = (int64_t*) Cache_Aligned_Allocate (SZ * sizeof (int64_t)); C = (int64_t*) Cache_Aligned_Allocate (SZ * sizeof (int64_t)); D = (int64_t*) Cache_Aligned_Allocate (SZ * sizeof (int64_t)); srandom (99); for (i=0; i<SZ; i++) { A[i] = random (); B[i] = random (); C[i] = random (); } map_allocate (1); // call the MAP routine subr (A, B, C, D, num, &tm, mapnum); printf ("%lld clocks\n", tm); for (i=0; i<num; i++) { int64_t vx, vy, res; fprintf (res_map, "%lld\n", D[i]); vx = C[i] * (A[i]>B[i] ? A[i]-B[i] : A[i]+B[i]); vy = vx + A[i]; vy = vy / 42; res = vy + C[i]; fprintf (res_cpu, "%lld\n", res); } map_free (1); exit(0); }
int main (int argc, char *argv[]) { FILE *res_map, *res_cpu; int i, j, D0, D1, num; int64_t *A, *B, *MB; int64_t tm, res, accum=0; int mapnum = 0; if ((res_map = fopen ("res_map", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_map'\n"); exit (1); } if ((res_cpu = fopen ("res_cpu", "w")) == NULL) { fprintf (stderr, "failed to open file 'res_cpu'\n"); exit (1); } if (argc < 3) { fprintf (stderr, "need two dimensions as args\n"); exit (1); } if (sscanf (argv[1], "%d", &D0) < 1) { fprintf (stderr, "need two dimensions as args\n"); exit (1); } if (sscanf (argv[2], "%d", &D1) < 1) { fprintf (stderr, "need two dimensions as args\n"); exit (1); } if ((D0*D1 < 1) || (D0*D1 > MAX_OBM_SIZE)) { fprintf (stderr, "number of elements must be in the range 1 through %d\n", MAX_OBM_SIZE); exit (1); } A = (int64_t*) Cache_Aligned_Allocate (D0*D1 * sizeof (int64_t)); B = (int64_t*) Cache_Aligned_Allocate (D0 * sizeof (int64_t)); MB = (int64_t*) Cache_Aligned_Allocate (D0 * sizeof (int64_t)); srandom (99); for (i=0; i<D0*D1; i++) { A[i] = random (); } for (i=0; i<D0; i++) { MB[i] = 0; for (j=0; j<D1; j++) MB[i] += A[i*D1+j]; } map_allocate (1); subr (A, B, D0, D1, &tm, mapnum); printf ("%lld clocks\n", tm); for (i=0; i<D0; i++) { fprintf (res_map, "%lld\n", B[i]); fprintf (res_cpu, "%lld\n", MB[i]); } map_free (1); exit(0); }