Skip to content

Commit

Permalink
Add output code for CSR
Browse files Browse the repository at this point in the history
  • Loading branch information
brucehow committed Sep 14, 2019
1 parent 906501f commit a120721
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
19 changes: 19 additions & 0 deletions matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,25 @@ int main(int argc, char *argv[]) {
struct CSR result = matrix_addition(admatrix, admatrix2);
end = clock();
routine_time = (double) (end - start) / CLOCKS_PER_SEC;

if (log) {
char *output_file = get_output_name(tm, "ad");
FILE *output = fopen(output_file, "w"); // sample file
if(output == NULL) {
fprintf(stderr, "matrix: failed to generate output file\n");
exit(EXIT_FAILURE);
}
write_details(output, filename, filename2, rows, cols, routine.type, result.type);
write_csr_data(output, result);
write_times(output, load_time, routine_time);
printf("matrix: successfully logged results to '%s'\n", output_file);
fclose(output);
free(output_file);
} else {
write_details(stdout, filename, filename2, rows, cols, routine.type, result.type);
write_csr_data(stdout, result);
write_times(stdout, load_time, routine_time);
}
break;
case TS:
break;
Expand Down
2 changes: 2 additions & 0 deletions matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ extern void write_times(FILE *fp, float load_time, float routine_time);
*/
extern void write_coo_data(FILE *fp, struct COO matrix);

extern void write_csr_data(FILE *fp, struct CSR matrix);

/**
* Writes the header details to the given file pointer
*
Expand Down
40 changes: 40 additions & 0 deletions output.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,43 @@ void write_coo_data(FILE *fp, struct COO matrix) {
}
fprintf(fp, "\n");
}

void write_csr_data(FILE *fp, struct CSR matrix) {
int elements = 0; // Number of non zero elements in each row
int pos = 0; // Position pointer
int count = 0; // Number of written values
int j = 0;
if (matrix.type == TYPE_INT) {
for (int i = 1; i < matrix.rows+1; i++) {
elements = matrix.ia[i] - matrix.ia[i-1];
pos = matrix.ia[i-1];
count = 0;
j = 0;
while (count < matrix.cols) {
if (elements == 0) { // Written all the elements
fprintf(fp, "0");
for (int j = 1; j < matrix.cols - count; j++) {
fprintf(fp, " 0");
}
if (i != matrix.rows) { // Write space if not last val
fprintf(fp, " ");
}
break; // Skip to next row
}
while (j != matrix.ja[pos]) {
fprintf(fp, "0 ");
j++;
count++;
}
fprintf(fp, "%d", matrix.nnz.i[pos++]); // Print the value
elements--;
j++;
count++;
if (count != matrix.cols || i != matrix.rows) {
fprintf(fp, " ");
}
}
}
}
fprintf(fp, "\n");
}

0 comments on commit a120721

Please sign in to comment.