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  struct prng_rand_t state;
45 
46  // Load matrix file
47  fd = open("494_bus_full.mtx", O_RDONLY);
48  assert( fd>=0 && "couldn't open matrix" );
49  status = fstat( fd, &file_info );
50  assert( status==0 && "couldn't get filesize of matrix" );
51  buffer = malloc(file_info.st_size+1);
52  buffer[file_info.st_size]=(char)0;
53  nbytes = 0;
54  do {
55  status = read(fd, buffer, file_info.st_size-nbytes);
56  assert(status>=0 && "Couldn't read from matrix file");
57  nbytes+=status;
58  } while( nbytes<file_info.st_size );
59  close(fd);
60 
61  // Parse matrix file
62  current = buffer;
63  next = strchr(current, '\n');// skip first two lines
64  *next = (char)0;
65  current = next+1;
66  for(i=0; i<NNZ; i++) {
67  next = strchr(current, '\n');
68  *next = (char)0;
69  current = next+1;
70  status = sscanf(current, "%d %d %lf", &coords[i][ROW], &coords[i][COL], &data.val[i]);
71  assert(status==3 && "Parse error in matrix file");
72  }
73 
74  // Sort by row
75  qsort(coords, NNZ, 2*sizeof(int), &compar);
76 
77  // Fill data structure
78  for(i=0; i<NNZ; i++)
79  data.cols[i] = coords[i][COL]-1;
80  memset(data.rowDelimiters, 0, (N+1)*sizeof(int));
81  for(i=0; i<NNZ; i++)
82  data.rowDelimiters[coords[i][ROW]-1+1] += 1; // count
83  // (-1 because matrix is 1-indexed, +1 because it's counting cells before it)
84  for(i=1; i<N+1; i++)
85  data.rowDelimiters[i] += data.rowDelimiters[i-1]; // scan
86 
87  // Set vector
88  prng_srand(1,&state);
89  for( i=0; i<N; i++ )
90  data.vec[i] = ((double)prng_rand(&state))/((double)PRNG_RAND_MAX);
91 
92  // Open and write
93  fd = open("input.data", O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
94  assert( fd>0 && "Couldn't open input data file" );
95  data_to_input(fd, (void *)(&data));
96 
97  return 0;
98 }
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 *))
int32_t rowDelimiters[N+1]
Definition: spmv.h:24
void data_to_input(int fd, void *vdata)
Definition: local_support.c:34
int32_t cols[NNZ]
Definition: spmv.h:23
#define NNZ
Definition: spmv.h:11
TYPE vec[N]
Definition: spmv.h:25
#define COL
Definition: generate.c:13
uint_fast16_t i
Definition: support.h:78
#define ROW
Definition: generate.c:12
#define N
Definition: dfdiv.c:60
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
TYPE val[NNZ]
Definition: spmv.h:22
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