Skip to content
Snippets Groups Projects
Commit f149662c authored by Arnold Sianturi's avatar Arnold Sianturi
Browse files

dijkstra_openmp.c is added

parent 64df4402
No related merge requests found
dijk 0 → 100755
File added
# include <stdlib.h>
# include <stdio.h>
# include <time.h>
# include <omp.h>
# define NV 3000
# define inf 99999
// main
int main ( int argc, char **argv);
// algoritma dijkstra pakai openmp
int *dijkstra_algorithm (int path_mat[NV][NV], int i_node, int N);
// algoritma untuk mencari jarak terdekat dari node-i ke tiap-tiap node
void nearest_dist ( int s, int e, int dist_mat[NV], int connected[NV], int *d, int *v );
// inisialisasi matrix
void init_mat (int path_mat[NV][NV], int N);
void timestamp ( void );
// update nilai matriks jarak
void dist_mat_update ( int s, int e, int mv, int connected[NV], int path_math[NV][NV], int dist_mat[NV] );
/******************************************************************************/
int main (int argc, char **argv) {
int N = atoi(argv[1]);
int i, j;
int *dist_mat;
int path_mat[NV][NV];
init_mat ( path_mat, N );
int x, y;
int result_mat[N][N];
clock_t begin = clock();
for (x=0; x<N; x++){
dist_mat = dijkstra_algorithm(path_mat, x, N);
for (y=0; y<N; y++){
result_mat[x][y]= dist_mat[y];
result_mat[y][x]= dist_mat[y];
}
for(int i=0; i<N; i++)
dist_mat[i] = 0;
}
clock_t end = clock();
double time_spent = (double)((end - begin)*1000) / CLOCKS_PER_SEC;
FILE* fp;
fp = fopen("output.txt", "w");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (j == N - 1)
{
fprintf(fp, "%d\n", result_mat[i][j]);
}
else
{
fprintf(fp, "%d ", result_mat[i][j]);
}
}
}
free ( dist_mat );
printf("Algorithm took %f ms to be executed.\n", time_spent);
return 0;
}
/******************************************************************************/
int *dijkstra_algorithm (int path_mat[NV][NV], int i_node, int N) {
int *connected;
int i;
int md;
int *dist_mat;
int mv;
int my_first;
int my_id;
int my_last;
int my_md;
int my_mv;
int my_step;
int nth;
connected = ( int * ) malloc ( N * sizeof ( int ) );
i=0;
if(i<i_node){
for (i=0; i<i_node; i++)
connected[i] = 0;
}
connected[i_node] = 1;
for ( i = i_node+1; i < N; i++ )
{
connected[i] = 0;
}
/*
Initial estimate of minimum distance is the 1-step distance.
*/
dist_mat = ( int * ) malloc ( N * sizeof ( int ) );
for ( i = 0; i < N; i++ )
{
dist_mat[i] = path_mat[i_node][i];
}
/*
Begin the parallel region.
*/
# pragma omp parallel private ( my_first, my_id, my_last, my_md, my_mv, my_step ) \
shared ( connected, md, dist_mat, mv, nth, path_mat )
{
my_id = omp_get_thread_num ( );
nth = omp_get_num_threads ( );
my_first = ( my_id * N) / nth;
my_last = ( ( my_id + 1 ) * N) / nth - 1;
# pragma omp single
{}
for ( my_step = 1; my_step < N; my_step++ )
{
# pragma omp single
{
md = inf;
mv = -1;
}
nearest_dist ( my_first, my_last, dist_mat, connected, &my_md, &my_mv );
# pragma omp critical
{
if ( my_md < md )
{
md = my_md;
mv = my_mv;
}
}
# pragma omp barrier
# pragma omp single
{
if ( mv != - 1 )
{
connected[mv] = 1;
}
}
# pragma omp barrier
if ( mv != -1 )
{
dist_mat_update ( my_first, my_last, mv, connected, path_mat, dist_mat );
}
#pragma omp barrier
}
# pragma omp single
{}
}
free ( connected );
return dist_mat;
}
/******************************************************************************/
void nearest_dist ( int s, int e, int dist_mat[NV], int connected[NV], int *d, int *v ) {
int i;
*d = inf;
*v = -1;
for ( i = s; i <= e; i++ )
{
if ( !connected[i] && ( dist_mat[i] < *d ) )
{
*d = dist_mat[i];
*v = i;
}
}
return;
}
/******************************************************************************/
void init_mat (int path_mat[NV][NV], int N) {
int i, j;
srand(13516001);
// #pragma omp parallel for num_threads(10)
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (i == j)
{
path_mat[i][j] = 0;
}
else if (path_mat[i][j] > 800) {
path_mat[i][j] = inf;
path_mat[j][i] = inf;
}
else {
int temp = rand() % 1000;
path_mat[i][j] = temp;
path_mat[j][i] = temp;
}
}
}
return;
}
/******************************************************************************/
void timestamp ( void ) {
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct tm *tm;
time_t now;
now = time ( NULL );
tm = localtime ( &now );
strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
printf ( "%s\n", time_buffer );
return;
# undef TIME_SIZE
}
/******************************************************************************/
void dist_mat_update ( int s, int e, int mv, int connected[NV], int path_mat[NV][NV], int dist_mat[NV] ) {
int i;
for ( i = s; i <= e; i++ )
{
if ( !connected[i] )
{
if ( path_mat[mv][i] < inf )
{
if ( dist_mat[mv] + path_mat[mv][i] < dist_mat[i] )
{
dist_mat[i] = dist_mat[mv] + path_mat[mv][i];
}
}
}
}
return;
}
\ No newline at end of file
This diff is collapsed.
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