source: proiecte/Traffic/Dijkstra.c @ 149

Last change on this file since 149 was 149, checked in by (none), 14 years ago
File size: 2.6 KB
Line 
1 
2
3#include <omp.h>
4#include <stdio.h>
5#include <sys/time.h>
6
7#define INFINITE 998
8#define CHUNKSIZE 1 /*defines the chunk size as 1 contiguous iteration*/
9
10int MAX;
11
12int allselected(int *selected)
13{
14    int i;
15    for(i=0;i<MAX;i++)
16        if(selected[i]==0)
17            return 0;
18    return 1;
19}
20void shortpath(int cost[][1000],int *preced,int *distance)
21{
22    int selected[1000]={0};
23    int current=0,i,k,dc,smalldist,newdist;
24    for(i=0;i<MAX;i++)
25       distance[i]=INFINITE;
26    selected[current]=1;
27    distance[0]=0;
28    current=0;
29    #pragma omp parallel private(k,smalldist,newdist,current)
30    {
31        while(!allselected(selected))
32        {
33                smalldist=INFINITE;
34                dc=distance[current];
35                #pragma omp parallel for schedule(dynamic, CHUNKSIZE)
36                for(i=0;i<MAX;i++)
37                {
38                        if(selected[i]==0)
39                        {
40                                newdist=dc+cost[current][i];
41                                if(newdist<distance[i])
42                                {
43                                        //#pragma omp critical
44                                        {
45                                                distance[i]=newdist;
46                                        }
47                                        preced[i]=current;
48                                }
49                                if(distance[i]<smalldist)
50                                {
51                                        //#pragma omp critical
52                                        {
53                                                smalldist=distance[i];
54                                        }
55                                k=i;
56                                }
57                        }
58                }
59                current=k;
60                //#pragma omp critical
61                {
62                        selected[current]=1;
63                }
64        }
65    }
66}
67
68int main()
69{
70            int th_id, nthreads;
71        struct timeval time1;
72        struct timeval time2;
73        int cost[1000][1000];
74
75        FILE * pFile;
76        int n,k,i,j;
77
78  i=0;j=0;
79  pFile = fopen ("Di1000.txt","r");
80  if(pFile != NULL)
81        fscanf (pFile, "%d", &n);
82  MAX = n;
83  for(i=0;i<MAX;i++)
84        for(j=0;j<MAX;j++)
85                cost[i][j]=INFINITE;
86  while(fscanf(pFile,"%d",&i)!=EOF)
87  {
88        fscanf (pFile, "%d", &j);
89        fscanf (pFile, "%d", &cost[i][j]);
90   }
91  fclose (pFile);
92
93int preced[1000]={0},distance[1000];
94        gettimeofday(&time1,NULL);
95    shortpath(cost,preced,distance);
96        gettimeofday(&time2,NULL);
97
98        printf("time: %ld\n",time2.tv_usec-time1.tv_usec);
99
100    return 0;
101}
102
103
Note: See TracBrowser for help on using the repository browser.