1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/time.h>
#include <limits.h>
#include <unistd.h>
/* Program Parameters */
#define MAXN 2000 /* Max value of N */
int N; /* Matrix size i.e rows * cols */
int procs; /* Number of processors to use */
/* Matrices and vectors */
volatile float A[MAXN][MAXN], B[MAXN], X[MAXN];
/* A * X = B, solve for X */
/* returns a seed for srand based on the time */
unsigned int time_seed() {
struct timeval t;
struct timezone tzdummy;
gettimeofday(&t, &tzdummy);
return (unsigned int)(t.tv_usec);
}
/* Set the program parameters from the command-line arguments */
void parameters(int argc, char **argv) {
int submit = 0; /* = 1 if submission parameters should be used */
int seed = 0; /* Random seed */
char uid[/*L_cuserid + 2*/50]; /*User name */
/* Read command-line arguments */
srand(time_seed()); /* Randomize */
if (argc != 3) {
if ( argc == 2 && !strcmp(argv[1], "submit") ) {
/* Use submission parameters */
submit = 1;
N = 4;
procs = 2;
printf("\nSubmission run for \"%s\".\n", getlogin()/*cuserid(uid)*/ );
//srand(randm());
}
else {
if (argc == 4) {
seed = atoi(argv[3]);
srand(seed);
printf("Random seed = %i\n", seed);
}
else {
printf("Usage: %s <matrix_dimension> <num_procs> [random seed]\n",
argv[0]);
printf(" %s submit\n", argv[0]);
exit(0);
}
}
}
/* Interpret command-line args */
if (!submit) {
N = atoi(argv[1]);
if (N < 1 || N > MAXN) {
printf("N = %i is out of range.\n", N);
exit(0);
}
procs = atoi(argv[2]);
if (procs < 1) {
printf("Warning: Invalid number of processors = %i. Using 1.\n", procs);
procs = 1;
}
// USE CORES?
/*if (procs > m_get_numprocs()) {
printf("Warning: %i processors requested; only %i available.\n",
procs, m_get_numprocs());
procs = m_get_numprocs();
}*/
}
/* Print parameters */
printf("\nMatrix dimension N = %i.\n", N);
printf("Number of processors = %i.\n", procs);
/* Set number of processors */
/*m_set_procs(procs);
CORES ?
*/
}
/* Initialize A and B (and X to 0.0s) */
void initialize_inputs() {
int row, col;
printf("\nInitializing...\n");
for (col = 0; col < N; col++) {
for (row = 0; row < N; row++) {
A[row][col] = (float)rand() / 32768.0;
}
B[col] = (float)rand() / 32768.0;
X[col] = 0.0;
}
}
/* Print input matrices */
void print_inputs() {
int row, col;
if (N < 10) {
printf("\nA =\n\t");
for (row = 0; row < N; row++) {
for (col = 0; col < N; col++) {
printf("%5.2f%s", A[row][col], (col < N-1) ? ", " : ";\n\t");
}
}
printf("\nB = [");
for (col = 0; col < N; col++) {
printf("%5.2f%s", B[col], (col < N-1) ? "; " : "]\n");
}
}
}
void main(int argc, char **argv) {
/* Timing variables */
struct timeval etstart, etstop; /* Elapsed times using gettimeofday() */
struct timezone tzdummy;
clock_t etstart2, etstop2; /* Elapsed times using times() */
unsigned long long usecstart, usecstop;
struct tms cputstart, cputstop; /* CPU times for my processes */
/* Process program parameters */
parameters(argc, argv);
/* Start Clock */
printf("\nStarting clock.\n");
gettimeofday(&etstart, &tzdummy);
etstart2 = times(&cputstart);
/* Initialize A and B */
initialize_inputs();
/* Print input matrices */
print_inputs();
/* Stop Clock */
gettimeofday(&etstop, &tzdummy);
etstop2 = times(&cputstop);
printf("Stopped clock.\n");
usecstart = (unsigned long long)etstart.tv_sec * 1000000 + etstart.tv_usec;
usecstop = (unsigned long long)etstop.tv_sec * 1000000 + etstop.tv_usec;
/* Display output */
/* Display timing results */
printf("\nElapsed time = %g ms.\n",
(float)(usecstop - usecstart)/(float)1000);
/*printf(" (%g ms according to times())\n",
* (etstop2 - etstart2) / (float)_CLK_TCK) * 1000);
*/
printf("(CPU times are accurate to the nearest %g ms)\n",
1.0/(float)(sysconf(_SC_CLK_TCK)) * 1000.0);//NEW
printf("My total CPU time for parent = %g ms.\n",
(float)( (cputstop.tms_utime + cputstop.tms_stime) -
(cputstart.tms_utime + cputstart.tms_stime) ) /
(float)(sysconf(_SC_CLK_TCK)) * 1000);
printf("My system CPU time for parent = %g ms.\n",
(float)(cputstop.tms_stime - cputstart.tms_stime) /
(float)(sysconf(_SC_CLK_TCK)) * 1000);
printf("My total CPU time for child processes = %g ms.\n",
(float)( (cputstop.tms_cutime + cputstop.tms_cstime) -
(cputstart.tms_cutime + cputstart.tms_cstime) ) /
(float)(sysconf(_SC_CLK_TCK)) * 1000);
/* Contrary to the man pages, this appears not to include the parent */
printf("--------------------------------------------\n");
}
|