Subversion Repositories programming

Rev

Rev 318 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 318 Rev 319
Line 28... Line 28...
28
 
28
 
29
#include <stdio.h>
29
#include <stdio.h>
30
#include <stdlib.h>
30
#include <stdlib.h>
31
#include <unistd.h>
31
#include <unistd.h>
32
#include <limits.h>
32
#include <limits.h>
-
 
33
#include <sys/timeb.h>
33
 
34
 
34
#include <mpi.h>
35
#include <mpi.h>
35
 
36
 
36
#define ROOT_NODE 0
37
#define ROOT_NODE 0
37
 
38
 
Line 61... Line 62...
61
    int c = -1;
62
    int c = -1;
62
 
63
 
63
    int size, seed, total, g_total;
64
    int size, seed, total, g_total;
64
    int *numbers;
65
    int *numbers;
65
 
66
 
-
 
67
    /* Declare timeb structs */
-
 
68
    struct timeb time1;
-
 
69
    struct timeb time2;
-
 
70
 
66
    MPI_Status status;
71
    MPI_Status status;
67
 
72
 
68
    MPI_Init (&argc, &argv);
73
    MPI_Init (&argc, &argv);
69
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
74
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
70
    MPI_Comm_rank (MPI_COMM_WORLD, &myid);
75
    MPI_Comm_rank (MPI_COMM_WORLD, &myid);
Line 132... Line 137...
132
 
137
 
133
        /* Calculate slice size */
138
        /* Calculate slice size */
134
        size = n/numprocs;
139
        size = n/numprocs;
135
    }
140
    }
136
 
141
 
-
 
142
    /* Start the clock */
-
 
143
    if (myid == ROOT_NODE)
-
 
144
        ftime (&time1);
-
 
145
 
137
    /* Broadcast size to all nodes */
146
    /* Broadcast size to all nodes */
138
    MPI_Bcast (&size, 1, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
147
    MPI_Bcast (&size, 1, MPI_INT, ROOT_NODE, MPI_COMM_WORLD);
139
 
148
 
140
    if (myid != ROOT_NODE)
149
    if (myid != ROOT_NODE)
141
    {
150
    {
Line 155... Line 164...
155
    total = find_seed_occurrances (numbers, size, seed);
164
    total = find_seed_occurrances (numbers, size, seed);
156
 
165
 
157
    /* Sum everyone's results */
166
    /* Sum everyone's results */
158
    MPI_Reduce (&total, &g_total, 1, MPI_INT, MPI_SUM, ROOT_NODE, MPI_COMM_WORLD);
167
    MPI_Reduce (&total, &g_total, 1, MPI_INT, MPI_SUM, ROOT_NODE, MPI_COMM_WORLD);
159
 
168
 
-
 
169
    /* Stop the clock */
-
 
170
    if (myid == ROOT_NODE)
-
 
171
        ftime (&time2);
-
 
172
 
160
    if (myid == ROOT_NODE)
173
    if (myid == ROOT_NODE)
161
    {
174
    {
-
 
175
        /* Print time taken */
-
 
176
        long time_taken = ((time2.time - time1.time) * 1000) + (time2.millitm - time1.millitm);
-
 
177
        printf ("Time taken: %d (milliseconds)\n", time_taken);
-
 
178
 
162
        /* Print the final status */
179
        /* Print the final status */
163
        printf ("Total occurrance of seed (%d) out of %d numbers: %d\n", seed, n, g_total);
180
        printf ("Total occurrance of seed (%d) out of %d numbers: %d\n", seed, n, g_total);
164
    }
181
    }
165
 
182
 
166
    free (numbers);
183
    free (numbers);