#include #include #include #include #include #include #include // this code is left uncommented as it is self explanatory or the same as the // parallel version bool allow_print; void printSquare(double **array, unsigned long dimension) { unsigned long i, j; for (i = 0; i < dimension; i++) { for (j = 0; j < dimension; j++) printf("%f ", array[i][j]); printf("\n"); } } long double time_seconds(struct timespec begin, struct timespec end) { long long sec_diff = end.tv_sec - begin.tv_sec; long long nsec_diff = end.tv_nsec - begin.tv_nsec; long long as_ns = sec_diff * 1000000000L + nsec_diff; return (long double) as_ns / 1000000000.0; } unsigned long solveArray(double **array, unsigned long dimension, double precision) { unsigned long i, j, k; bool solved; struct timespec begin, end; k = 0; clock_gettime(CLOCK_MONOTONIC, &begin); while (1) { k++; solved = true; for (i = 1; i < dimension - 1; i++) { for (j = 1; j < dimension - 1; j++) { double previous = array[i][j]; double avg = (array[i][j - 1] + array[i][j + 1] + array[i - 1][j] + array[i + 1][j]) / 4.0; array[i][j] = avg; if (solved == true && fabs(avg - previous) > precision) { solved = false; } } } if (solved == true) { clock_gettime(CLOCK_MONOTONIC, &end); if (allow_print) printf("time taken: %Lf\n", time_seconds(begin, end)); return k; } } } double **constructBigArray(double top, double bottom, double left, double right, long unsigned int n) { long unsigned int i, j, k; double **arr = (double **) malloc(n * sizeof(double *)); for (k = 0; k < n; k++) { arr[k] = (double *) malloc(n * sizeof(double)); } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i == 0) { if (j == 0 || j == n - 1) arr[i][j] = 0; else arr[i][j] = top; } else if (i == n - 1) { if (j == 0 || j == n - 1) arr[i][j] = 0; else arr[i][j] = bottom; } else if (j == 0) { arr[i][j] = left; } else if (j == n - 1) { arr[i][j] = right; } else { arr[i][j] = 0; } } } return arr; } typedef struct arg_data { double precision; unsigned long dimension; double top_start; double bottom_start; double left_start; double right_start; } ARG_DATA_T; int argsContain(const char *val, char **arr, int size) { int i; for (i = 0; i < size; i++) { if (strcmp(arr[i], val) == 0) return i; } return -1; } ARG_DATA_T *parseArgs(int argc, char **argv) { int tmp; ARG_DATA_T *args = (ARG_DATA_T *) malloc(sizeof(ARG_DATA_T)); allow_print = true; if ((tmp = argsContain("-p", argv, argc)) >= 0 || (tmp = argsContain("--precision", argv, argc)) >= 0) { args->precision = strtod(argv[tmp + 1], NULL); } else { args->precision = 0.00001; } if ((tmp = argsContain("-d", argv, argc)) >= 0 || (tmp = argsContain("--dimension", argv, argc)) >= 0) { args->dimension = (unsigned long) strtol(argv[tmp + 1], NULL, 10); } else { args->dimension = 10; } if ((tmp = argsContain("-t", argv, argc)) >= 0 || (tmp = argsContain("--top", argv, argc)) >= 0) { args->top_start = strtod(argv[tmp + 1], NULL); } else { args->top_start = 1; } if ((tmp = argsContain("-l", argv, argc)) >= 0 || (tmp = argsContain("--left", argv, argc)) >= 0) { args->left_start = strtod(argv[tmp + 1], NULL); } else { args->left_start = 2; } if ((tmp = argsContain("-r", argv, argc)) >= 0 || (tmp = argsContain("--right", argv, argc)) >= 0) { args->right_start = strtod(argv[tmp + 1], NULL); } else { args->right_start = 4; } if ((tmp = argsContain("-b", argv, argc)) >= 0 || (tmp = argsContain("--bottom", argv, argc)) >= 0) { args->bottom_start = strtod(argv[tmp + 1], NULL); } else { args->bottom_start = 3; } if ((tmp = argsContain("-q", argv, argc)) > 0 || (tmp = argsContain("--quiet", argv, argc)) >= 0) { allow_print = false; } if (argsContain("-h", argv, argc) >= 0 || argsContain("--help", argv, argc) >= 0) { printf("Usage: %s [-p N] [-d N] [-t N] [-l N] [-r N] [-b N]\n", argv[0]); printf("Constructs a 2D array of doubles, with values from arguments " "of size dimension^2 and performs relaxation method" " to given precision.\n"); printf("Options:\n"); printf("-p/--precision: A double value to specify precision. " "Default: 0.0001\n"); printf("-d/--dimension: A long value to specify width and height of " "grid. Default: 10\n"); printf("-t/--top: A double indicating the values along the top of the " "array. Default: 1.0\n"); printf("-l/--left: A double indicating the values along the left side" " of the array. Default: 2.0\n"); printf("-b/--bottom: A double indicating the values along the bottom " "of the array. Default: 3.0\n"); printf("-r/--right: A double indicating the values along the right " "side of the array. Default: 4.0\n"); printf("-q/--quiet: Disables all printing except the result.\n"); exit(0); } return args; } int main(int argc, char **argv) { unsigned long iter; ARG_DATA_T *args; args = parseArgs(argc, argv); double **array = constructBigArray(args->top_start, args->bottom_start, args->left_start, args->right_start, args->dimension); iter = solveArray(array, args->dimension, args->precision); if (allow_print) printf("completed with iters: %lu\n", iter); printSquare(array, args->dimension); }