PandA-2024.02
generate.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <assert.h>
9 
10 #include "spmv.h"
11 
12 #define ROW 0
13 #define COL 1
14 
15 // Row first, then column comparison
16 int compar(const void *v_lhs, const void *v_rhs)
17 {
18  int row_lhs = ((int *)v_lhs)[ROW];
19  int row_rhs = ((int *)v_rhs)[ROW];
20  int col_lhs = ((int *)v_lhs)[COL];
21  int col_rhs = ((int *)v_rhs)[COL];
22 
23  if( row_lhs==row_rhs ) {
24  if( col_lhs==col_rhs )
25  return 0;
26  else if( col_lhs<col_rhs )
27  return -1;
28  else
29  return 1;
30  } else if( row_lhs<row_rhs ) {
31  return -1;
32  } else {
33  return 1;
34  }
35 }
36 
37 int main(int argc, char **argv)
38 {
39  struct bench_args_t data;
40  struct stat file_info;
41  char *current, *next, *buffer;
42  int status, i, fd, nbytes;
43  int coords[NNZ][2]; // row, col
44  TYPE vals[NNZ];
45  int row_fill[N];
46  int row, index;
47  struct prng_rand_t state;
48 
49  // Load matrix file
50  fd = open("494_bus_full.mtx", O_RDONLY);
51  assert( fd>=0 && "couldn't open matrix" );
52  status = fstat( fd, &file_info );
53  assert( status==0 && "couldn't get filesize of matrix" );
54  buffer = malloc(file_info.st_size+1);
55  buffer[file_info.st_size]=(char)0;
56  nbytes = 0;
57  do {
58  status = read(fd, buffer, file_info.st_size-nbytes);
59  assert(status>=0 && "Couldn't read from matrix file");
60  nbytes+=status;
61  } while( nbytes<file_info.st_size );
62  close(fd);
63 
64  // Parse matrix file
65  current = buffer;
66  next = strchr(current, '\n');// skip first two lines
67  *next = (char)0;
68  current = next+1;
69  for(i=0; i<NNZ; i++) {
70  next = strchr(current, '\n');
71  *next = (char)0;
72  current = next+1;
73  status = sscanf(current, "%d %d %lf", &coords[i][ROW], &coords[i][COL], &vals[i]);
74  assert(status==3 && "Parse error in matrix file");
75  }
76 
77  // Sort by row
78  qsort(coords, NNZ, 2*sizeof(int), &compar);
79 
80  // Fill data structure
81  memset(row_fill, 0, N*sizeof(int)); // fill helper array
82  memset(data.cols, 0, N*L*sizeof(int)); // value doesn't really matter
83  memset(data.nzval, 0, N*L*sizeof(TYPE)); // value DOES matter (must be 0)
84  for(i=0; i<NNZ; i++) {
85  row = coords[i][ROW]-1; // matrix is 1-indexed
86  index = L*row + row_fill[row];
87  data.nzval[index] = vals[i];
88  data.cols[index] = coords[i][COL]-1; // matrix is 1-indexed
89  ++row_fill[row];
90  }
91 
92  // Set vector
93  prng_srand(1,&state);
94  for(i=0; i<N; i++)
95  data.vec[i] = ((double)prng_rand(&state))/((double)PRNG_RAND_MAX);
96 
97  // Open and write
98  fd = open("input.data", O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
99  assert( fd>0 && "Couldn't open input data file" );
100  data_to_input(fd, (void *)(&data));
101 
102  return 0;
103 }
static uint64_t prng_rand(struct prng_rand_t *state)
Definition: support.h:85
void qsort(void *base, size_t nel, size_t width, int(*cmp)(const void *, const void *))
void data_to_input(int fd, void *vdata)
Definition: local_support.c:34
#define ROW
Definition: generate.c:12
#define TYPE
Definition: backprop.h:21
int32_t cols[NNZ]
Definition: spmv.h:23
TYPE nzval[N *L]
Definition: spmv.h:22
#define NNZ
Definition: spmv.h:11
TYPE vec[N]
Definition: spmv.h:25
uint_fast16_t i
Definition: support.h:78
#define COL
Definition: generate.c:13
#define index(x, y)
Definition: Keccak.c:74
#define N
Definition: dfdiv.c:60
#define L
Definition: spmv.h:13
int main(int argc, char **argv)
Definition: generate.c:12
int compar(const void *v_lhs, const void *v_rhs)
Definition: generate.c:16
static void prng_srand(uint64_t seed, struct prng_rand_t *state)
Definition: support.h:106
short int read(short int *data)
Definition: read.c:3
#define PRNG_RAND_MAX
Definition: support.h:82

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13