Subversion Repositories programming

Rev

Rev 303 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 303 Rev 304
Line 54... Line 54...
54
    }
54
    }
55
}
55
}
56
 
56
 
57
int main (int argc, char *argv[])
57
int main (int argc, char *argv[])
58
{
58
{
59
    int numprocs, myid;
59
    int numprocs, myid, i;
60
    int n = 6400;
60
    int n = -1;
61
    int i;
61
    int c = -1;
62
 
62
 
63
    int size;
63
    int size;
64
    int *numbers;
64
    int *numbers;
65
 
65
 
66
    int max = INT_MIN;
66
    int max = INT_MIN;
Line 74... Line 74...
74
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
74
    MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
75
    MPI_Comm_rank (MPI_COMM_WORLD, &myid);
75
    MPI_Comm_rank (MPI_COMM_WORLD, &myid);
76
 
76
 
77
    if (myid == ROOT_NODE)
77
    if (myid == ROOT_NODE)
78
    {
78
    {
-
 
79
        /* Parse the command line options */
-
 
80
        opterr = 0;
-
 
81
 
-
 
82
        while ((c = getopt (argc, argv, "n:")) != -1)
-
 
83
        {
-
 
84
            switch (c)
-
 
85
            {
-
 
86
                /* Try to get -n option */
-
 
87
                case 'n':
-
 
88
                    n = atoi (optarg);
-
 
89
                    break;
-
 
90
 
-
 
91
                /* Catch bad options */
-
 
92
                case '?':
-
 
93
                    if (isprint (optopt))
-
 
94
                        fprintf (stderr, "Unknown option '-%c'.\n", optopt);
-
 
95
                    else
-
 
96
                        fprintf (stderr, "Unknown option character '\\x%x'.\n", optopt);
-
 
97
 
-
 
98
                    return 1;
-
 
99
                    break;
-
 
100
                default:
-
 
101
                    abort ();
-
 
102
            }
-
 
103
        }
-
 
104
 
-
 
105
        /* Seed the random number generator */
-
 
106
        srand (time(NULL));
-
 
107
 
-
 
108
        /* Check if the n value is valid */
-
 
109
        if (n<=0)
-
 
110
        {
-
 
111
            fprintf (stderr, "Bad value for n, must be greater than 0\n");
-
 
112
            fprintf (stderr, "Please specify the '-n NUMBER' option\n");
-
 
113
            return 1;
-
 
114
        }
-
 
115
 
-
 
116
        /* Allocate memory for the random number array */
79
        if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
117
        if ((numbers = (int*) malloc (n * sizeof(int))) == NULL)
80
        {
118
        {
81
            puts ("Out of memory");
119
            puts ("Out of memory");
82
            return 1;
120
            return 1;
83
        }
121
        }
84
 
122
 
-
 
123
        /* Generate random numbers, staying in the range [0,10n) */
85
        for (i=0; i<n; i++)
124
        for (i=0; i<n; i++)
86
            numbers[i] = i;
125
            numbers[i] = (int) ((n*10.0) * rand() / (RAND_MAX + 1.0));
87
 
126
 
88
        /* Calculate slice size */
127
        /* Calculate slice size */
89
        size = n/numprocs;
128
        size = n/numprocs;
90
 
129
 
91
        /* Send out n to everyone */
130
        /* Send out n to everyone */
Line 116... Line 155...
116
        }
155
        }
117
 
156
 
118
        printf ("final max: %d\n", max);
157
        printf ("final max: %d\n", max);
119
        printf ("final min: %d\n", min);
158
        printf ("final min: %d\n", min);
120
    }
159
    }
121
    else
160
    else // not ROOT_NODE
122
    {
161
    {
123
        /* Recieve size of the data */
162
        /* Recieve size of the data */
124
        MPI_Recv (&size, 1, MPI_INT, ROOT_NODE, TAG_SIZE, MPI_COMM_WORLD, &status);
163
        MPI_Recv (&size, 1, MPI_INT, ROOT_NODE, TAG_SIZE, MPI_COMM_WORLD, &status);
125
 
164
 
126
        /* Allocate memory to store the data */
165
        /* Allocate memory to store the data */