Skip to content
Snippets Groups Projects
Commit 171ee97d authored by Barbariansyah's avatar Barbariansyah
Browse files

first init

parent 0695876f
No related merge requests found
dist
.vscode
hosts.dev.txt
*.o
.DS_Store
\ No newline at end of file
#include <stdio.h>
#include "utils/matrix.h"
#include "utils/dijkstra.h"
long int *calculate_sub_matrix(long int *matrix, int node_count, int thread_count);
int main(int argc, char *argv[])
{
int thread_count = strtol(argv[1], NULL, 10);
char print_dist;
setvbuf(stdout, NULL, _IONBF, 0);
srand(13517003);
if (argc < 2)
{
printf("usage: main <num_of_nodes>");
exit(-1);
}
int node_count = atoi(argv[2]);
long int *adj_matrix = create_adj_matrix(node_count, node_count);
double start_time = omp_get_wtime();
long int *sub_dist = calculate_sub_matrix(adj_matrix, node_count, thread_count);
double time = omp_get_wtime() - start_time;
printf("Elapsed Time : %f\n", time*1000000);
printf("Print distances to stdout? [y/N] ");
scanf("%c", &print_dist);
print_matrix_to_file(sub_dist, node_count, node_count, argv[3]);
if (print_dist == 'y' || print_dist == 'Y')
{
print_matrix(sub_dist, node_count, node_count);
}
free(sub_dist);
free(adj_matrix);
}
long int *calculate_sub_matrix(long int *matrix, int node_count, int thread_count)
{
long int *sub_dist = (long int *)malloc(node_count * node_count * sizeof(long int));
for (int i = 0; i < node_count; i++)
{
long int *temp_dist = dijkstra(matrix, i, node_count);
for (int j = 0; j < node_count; j++)
{
set_el(sub_dist, node_count, j, i, temp_dist[j]);
}
}
return sub_dist;
}
#include "dijkstra.h"
long int get_idx_min_dist(long int *dist, short *processed, int len)
{
long int min = LONG_MAX;
int idx;
for (int i = 0; i < len; i++)
{
if (!processed[i] && dist[i] < min)
{
idx = i;
min = dist[i];
}
}
return idx;
}
long int *dijkstra(long int *adj_matrix, int src, int size)
{
long int *dist = (long int *)malloc(size * sizeof(long int));
short processed[size];
for (int i = 0; i < size; i++)
{
dist[i] = LONG_MAX;
processed[i] = FALSE;
}
dist[src] = 0L;
for (int j = 0; j < size; j++)
{
int u = get_idx_min_dist(dist, processed, size);
processed[u] = TRUE;
for (int i = 0; i < size; i++)
{
if (get_el(adj_matrix, size, i, u) == 0 && u != i)
{
continue;
}
long int next_dist = dist[u] + get_el(adj_matrix, size, i, u);
if (next_dist < dist[i])
{
dist[i] = next_dist;
}
}
}
return dist;
}
\ No newline at end of file
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "matrix.h"
#define TRUE 1
#define FALSE 0
long int *dijkstra(long int *adj_matrix, int src, int size);
#include "matrix.h"
void print_array(long int *array, int width)
{
for (int i = 0; i < width; i++)
{
printf("%10ld\t", array[i]);
}
printf("\n");
}
void print_matrix(long int *matrix, int width, int height)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
printf("%10ld\t", matrix[i * width + j]);
}
printf("\n");
}
}
void print_matrix_to_file(long int *matrix, int width, int height, char *filename)
{
FILE *fp;
fp = fopen(filename, "w+");
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
fprintf(fp, "%10ld\t", matrix[i * width + j]);
}
fprintf(fp, "\n");
}
fclose(fp);
}
long int *create_adj_matrix(int width, int height)
{
long int *matrix = (long int *)malloc(width * height * sizeof(long int));
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == j)
matrix[i * width + j] = 0;
else if (i < j)
{
long int temp = (long int)rand();
matrix[i * width + j] = temp;
matrix[j * width + i] = temp;
}
}
}
return matrix;
}
long int get_el(long int *matrix, int width, int x, int y)
{
return matrix[y * width + x];
}
void set_el(long int *matrix, int width, int x, int y, long int value)
{
matrix[y * width + x] = value;
}
#include <stdio.h>
#include <stdlib.h>
void print_array(long int *array, int width);
void print_matrix(long int *matrix, int width, int height);
void print_matrix_to_file(long int *matrix, int width, int height, char *filename);
long int get_el(long int *matrix, int width, int x, int y);
void set_el(long int *matrix, int width, int x, int y, long int value);
long int *create_adj_matrix(int width, int height);
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment