is71 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
is71-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,201 @@
1
+ Metadata-Version: 2.4
2
+ Name: is71
3
+ Version: 0.1.0
4
+ Summary: C source and sample runs for the DC lab OpenMP and MPI exercises
5
+ Project-URL: Repository, https://github.com/atharvsgit/dc
6
+ Requires-Python: >=3.9
7
+ Description-Content-Type: text/markdown
8
+
9
+ # DC lab practical
10
+
11
+ ## Use the is71 command
12
+
13
+ Once version 0.1.0 is published to PyPI, install it in a Python 3.9+ virtual
14
+ environment.
15
+
16
+ ```bash
17
+ pip install is71
18
+ is71 src a1a
19
+ is71 src a4
20
+ is71 src b1 > b1.c
21
+ ```
22
+
23
+ Every program includes its compile and run commands, sample input, and
24
+ expected output. Use `is71 src --help` to list the exercise IDs. The programs
25
+ are bundled with the package, so the command works offline after installation.
26
+
27
+ ## Publish to PyPI
28
+
29
+ From the repository root, build and check the release files in a fresh
30
+ directory. The Vercel build uses `dist/` separately. On Ubuntu, install
31
+ `python3-venv` if Python reports that `ensurepip` is unavailable.
32
+
33
+ ```bash
34
+ python3 -m venv .venv
35
+ source .venv/bin/activate
36
+ python -m pip install build twine
37
+ release_dir=$(mktemp -d)
38
+ python -m build --outdir "$release_dir"
39
+ python -m twine check --strict "$release_dir"/*
40
+ ```
41
+
42
+ Create a PyPI API token with account-wide scope for the first upload, then run
43
+ the following and paste the token when Twine prompts for a password.
44
+
45
+ ```bash
46
+ python -m twine upload --username __token__ "$release_dir"/*
47
+ ```
48
+
49
+ For later releases, bump the version in `pyproject.toml` and use a
50
+ project-scoped token.
51
+
52
+ ## Download from Vercel
53
+
54
+ Each exercise has a short URL that returns the C source, followed by the
55
+ compile and run commands, sample input, and expected output in a C comment.
56
+ For example, `/a1a` opens Part A, question 1a.
57
+
58
+ ```bash
59
+ curl https://YOUR-SITE.vercel.app/a1a
60
+ ```
61
+
62
+ Add `-o a1a.c` to save the code, then follow the commands at the bottom of
63
+ the file. The homepage lists every route and shows a command with the current
64
+ hostname.
65
+
66
+ To deploy, import `atharvsgit/dc` in Vercel and use the repository root.
67
+ `vercel.json` sets the build command and output directory. Use the production
68
+ domain and allow unauthenticated access if you want curl to work without login.
69
+
70
+ ## Run from the repository
71
+
72
+ Open a terminal and enter the lab folder.
73
+
74
+ ```bash
75
+ cd "$HOME/Desktop/dc lab prac"
76
+ ```
77
+
78
+ ## Part A · OpenMP
79
+
80
+ Compile and run one pair of commands at a time. Each run below supplies sample input with `printf`. To type the input yourself, run the part of the line after `|`.
81
+
82
+ ### A1a · Parallel sum
83
+
84
+ ```bash
85
+ gcc -O2 -fopenmp openmp/a1a_sum.c -o a1a
86
+ printf '5\n1 2 3 4 5\n' | OMP_NUM_THREADS=4 ./a1a
87
+ ```
88
+
89
+ Expect `Sum = 15`. Remember `parallel for reduction(+:sum)`. The first number is the array size, with a limit of 100.
90
+
91
+ ### A1b · Loop scheduling
92
+
93
+ ```bash
94
+ gcc -O2 -fopenmp openmp/a1b_schedule.c -o a1b
95
+ printf '4\n' | OMP_DYNAMIC=FALSE OMP_NUM_THREADS=2 OMP_SCHEDULE=static,2 ./a1b
96
+ ```
97
+
98
+ Expect thread 0 to get iterations 0–1 and thread 1 to get 2–3. Try 9 iterations to see chunks assigned round-robin. Remember `schedule(runtime)` reads `OMP_SCHEDULE`.
99
+
100
+ ### A2 · Sequential and parallel merge sort
101
+
102
+ ```bash
103
+ gcc -O2 -fopenmp openmp/a2_merge_sort.c -o a2
104
+ printf '1000000\n' | OMP_DYNAMIC=FALSE ./a2
105
+ ```
106
+
107
+ The program generates the array from the size you enter and gives both runs identical data. Try 8 to see the sorted values. Remember `parallel sections` sorts two halves, then the main thread merges them.
108
+
109
+ ### A3a · Fibonacci with tasks
110
+
111
+ ```bash
112
+ gcc -O2 -fopenmp openmp/a3a_fibonacci.c -o a3a
113
+ printf '10\n' | OMP_NUM_THREADS=4 ./a3a
114
+ ```
115
+
116
+ Expect `0 1 1 2 3 5 8 13 21 34`. Remember `single` creates the tasks and `depend` makes each task wait for the two terms it reads. The program accepts up to 94 terms because it uses unsigned 64-bit arithmetic.
117
+
118
+ ### A3b · Parallel maximum
119
+
120
+ ```bash
121
+ gcc -O2 -fopenmp openmp/a3b_max.c -o a3b
122
+ printf '5\n-8 -2 -5 -9 -3\n' | OMP_NUM_THREADS=4 ./a3b
123
+ ```
124
+
125
+ Expect `Maximum = -2`. Remember `reduction(max:maximum)` with `INT_MIN` as the starting value. The first number is the array size, with a limit of 100.
126
+
127
+ ### A4 · Estimate π
128
+
129
+ ```bash
130
+ gcc -O2 -fopenmp openmp/a4_pi.c -o a4
131
+ printf '10000000\n' | OMP_NUM_THREADS=4 ./a4
132
+ ```
133
+
134
+ Expect both estimates to be near 3.14159265359. This uses the midpoint rule for the integral of `4 / (1 + x*x)` from 0 to 1. Remember `reduction(+:parallel_sum)` gives each thread a private accumulator and combines the results at the end.
135
+
136
+ ### A5 · Primes
137
+
138
+ ```bash
139
+ gcc -O2 -fopenmp openmp/a5_primes.c -o a5
140
+ printf '30\n' | OMP_NUM_THREADS=4 ./a5
141
+ printf '1000000\n' | OMP_NUM_THREADS=4 ./a5
142
+ ```
143
+
144
+ The small run lists 10 primes, from 2 through 29. The larger run reports 78,498 primes and omits the list. Remember each loop iteration tests one candidate independently.
145
+
146
+ For A2, A4, and A5, record the printed serial and parallel times in seconds. The reported difference is serial time minus parallel time. Timings vary across runs.
147
+
148
+ ## Part B · MPI
149
+
150
+ On Ubuntu, install the compiler wrapper and launcher if `command -v mpicc mpirun` cannot find them.
151
+
152
+ ```bash
153
+ sudo apt install openmpi-bin libopenmpi-dev
154
+ ```
155
+
156
+ Compile and run each program as follows. `-np 4` starts four processes on the local machine.
157
+
158
+ ### B1 · Scatter and gather
159
+
160
+ ```bash
161
+ mpicc mpi/b1_scatter_gather.c -o b1
162
+ mpirun -np 4 ./b1
163
+ ```
164
+
165
+ Expect `Gathered squares: 1 4 9 16`. Remember scatter sends each rank one number, and gather returns the squares to rank 0.
166
+
167
+ ### B2 · Reduce and allreduce
168
+
169
+ ```bash
170
+ mpicc mpi/b2_reduce.c -o b2
171
+ mpirun -np 4 ./b2
172
+ ```
173
+
174
+ Expect MAX = 4, MIN = 1, SUM = 10, and PROD = 24. Remember reduce gives the result to rank 0, and allreduce gives it to every rank.
175
+
176
+ ### B3 · Send, receive, and broadcast
177
+
178
+ ```bash
179
+ mpicc mpi/b3_send_recv_bcast.c -o b3
180
+ mpirun -np 4 ./b3
181
+ ```
182
+
183
+ Expect rank 1 to receive 42 from rank 0, then every rank to report the broadcast value 100. Remember send and receive match on a tag, and every rank calls broadcast with the same root.
184
+
185
+ ### B4 · Three messages to the root
186
+
187
+ ```bash
188
+ mpicc mpi/b4_messages.c -o b4
189
+ mpirun -np 4 ./b4
190
+ ```
191
+
192
+ Expect rank 0 to print Bangalore, Ramaiah Institute of Technology, and Information Science and Engineering. Remember `strlen(message) + 1` sends the string terminator too.
193
+
194
+ ### B5 · Barrier and squares
195
+
196
+ ```bash
197
+ mpicc mpi/b5_barrier.c -o b5
198
+ mpirun -np 4 ./b5
199
+ ```
200
+
201
+ Expect squares 1, 4, 9, and 16. Each rank finishes its calculation before any rank passes the barrier. Lines printed by different ranks can appear in any order.
is71-0.1.0/README.md ADDED
@@ -0,0 +1,193 @@
1
+ # DC lab practical
2
+
3
+ ## Use the is71 command
4
+
5
+ Once version 0.1.0 is published to PyPI, install it in a Python 3.9+ virtual
6
+ environment.
7
+
8
+ ```bash
9
+ pip install is71
10
+ is71 src a1a
11
+ is71 src a4
12
+ is71 src b1 > b1.c
13
+ ```
14
+
15
+ Every program includes its compile and run commands, sample input, and
16
+ expected output. Use `is71 src --help` to list the exercise IDs. The programs
17
+ are bundled with the package, so the command works offline after installation.
18
+
19
+ ## Publish to PyPI
20
+
21
+ From the repository root, build and check the release files in a fresh
22
+ directory. The Vercel build uses `dist/` separately. On Ubuntu, install
23
+ `python3-venv` if Python reports that `ensurepip` is unavailable.
24
+
25
+ ```bash
26
+ python3 -m venv .venv
27
+ source .venv/bin/activate
28
+ python -m pip install build twine
29
+ release_dir=$(mktemp -d)
30
+ python -m build --outdir "$release_dir"
31
+ python -m twine check --strict "$release_dir"/*
32
+ ```
33
+
34
+ Create a PyPI API token with account-wide scope for the first upload, then run
35
+ the following and paste the token when Twine prompts for a password.
36
+
37
+ ```bash
38
+ python -m twine upload --username __token__ "$release_dir"/*
39
+ ```
40
+
41
+ For later releases, bump the version in `pyproject.toml` and use a
42
+ project-scoped token.
43
+
44
+ ## Download from Vercel
45
+
46
+ Each exercise has a short URL that returns the C source, followed by the
47
+ compile and run commands, sample input, and expected output in a C comment.
48
+ For example, `/a1a` opens Part A, question 1a.
49
+
50
+ ```bash
51
+ curl https://YOUR-SITE.vercel.app/a1a
52
+ ```
53
+
54
+ Add `-o a1a.c` to save the code, then follow the commands at the bottom of
55
+ the file. The homepage lists every route and shows a command with the current
56
+ hostname.
57
+
58
+ To deploy, import `atharvsgit/dc` in Vercel and use the repository root.
59
+ `vercel.json` sets the build command and output directory. Use the production
60
+ domain and allow unauthenticated access if you want curl to work without login.
61
+
62
+ ## Run from the repository
63
+
64
+ Open a terminal and enter the lab folder.
65
+
66
+ ```bash
67
+ cd "$HOME/Desktop/dc lab prac"
68
+ ```
69
+
70
+ ## Part A · OpenMP
71
+
72
+ Compile and run one pair of commands at a time. Each run below supplies sample input with `printf`. To type the input yourself, run the part of the line after `|`.
73
+
74
+ ### A1a · Parallel sum
75
+
76
+ ```bash
77
+ gcc -O2 -fopenmp openmp/a1a_sum.c -o a1a
78
+ printf '5\n1 2 3 4 5\n' | OMP_NUM_THREADS=4 ./a1a
79
+ ```
80
+
81
+ Expect `Sum = 15`. Remember `parallel for reduction(+:sum)`. The first number is the array size, with a limit of 100.
82
+
83
+ ### A1b · Loop scheduling
84
+
85
+ ```bash
86
+ gcc -O2 -fopenmp openmp/a1b_schedule.c -o a1b
87
+ printf '4\n' | OMP_DYNAMIC=FALSE OMP_NUM_THREADS=2 OMP_SCHEDULE=static,2 ./a1b
88
+ ```
89
+
90
+ Expect thread 0 to get iterations 0–1 and thread 1 to get 2–3. Try 9 iterations to see chunks assigned round-robin. Remember `schedule(runtime)` reads `OMP_SCHEDULE`.
91
+
92
+ ### A2 · Sequential and parallel merge sort
93
+
94
+ ```bash
95
+ gcc -O2 -fopenmp openmp/a2_merge_sort.c -o a2
96
+ printf '1000000\n' | OMP_DYNAMIC=FALSE ./a2
97
+ ```
98
+
99
+ The program generates the array from the size you enter and gives both runs identical data. Try 8 to see the sorted values. Remember `parallel sections` sorts two halves, then the main thread merges them.
100
+
101
+ ### A3a · Fibonacci with tasks
102
+
103
+ ```bash
104
+ gcc -O2 -fopenmp openmp/a3a_fibonacci.c -o a3a
105
+ printf '10\n' | OMP_NUM_THREADS=4 ./a3a
106
+ ```
107
+
108
+ Expect `0 1 1 2 3 5 8 13 21 34`. Remember `single` creates the tasks and `depend` makes each task wait for the two terms it reads. The program accepts up to 94 terms because it uses unsigned 64-bit arithmetic.
109
+
110
+ ### A3b · Parallel maximum
111
+
112
+ ```bash
113
+ gcc -O2 -fopenmp openmp/a3b_max.c -o a3b
114
+ printf '5\n-8 -2 -5 -9 -3\n' | OMP_NUM_THREADS=4 ./a3b
115
+ ```
116
+
117
+ Expect `Maximum = -2`. Remember `reduction(max:maximum)` with `INT_MIN` as the starting value. The first number is the array size, with a limit of 100.
118
+
119
+ ### A4 · Estimate π
120
+
121
+ ```bash
122
+ gcc -O2 -fopenmp openmp/a4_pi.c -o a4
123
+ printf '10000000\n' | OMP_NUM_THREADS=4 ./a4
124
+ ```
125
+
126
+ Expect both estimates to be near 3.14159265359. This uses the midpoint rule for the integral of `4 / (1 + x*x)` from 0 to 1. Remember `reduction(+:parallel_sum)` gives each thread a private accumulator and combines the results at the end.
127
+
128
+ ### A5 · Primes
129
+
130
+ ```bash
131
+ gcc -O2 -fopenmp openmp/a5_primes.c -o a5
132
+ printf '30\n' | OMP_NUM_THREADS=4 ./a5
133
+ printf '1000000\n' | OMP_NUM_THREADS=4 ./a5
134
+ ```
135
+
136
+ The small run lists 10 primes, from 2 through 29. The larger run reports 78,498 primes and omits the list. Remember each loop iteration tests one candidate independently.
137
+
138
+ For A2, A4, and A5, record the printed serial and parallel times in seconds. The reported difference is serial time minus parallel time. Timings vary across runs.
139
+
140
+ ## Part B · MPI
141
+
142
+ On Ubuntu, install the compiler wrapper and launcher if `command -v mpicc mpirun` cannot find them.
143
+
144
+ ```bash
145
+ sudo apt install openmpi-bin libopenmpi-dev
146
+ ```
147
+
148
+ Compile and run each program as follows. `-np 4` starts four processes on the local machine.
149
+
150
+ ### B1 · Scatter and gather
151
+
152
+ ```bash
153
+ mpicc mpi/b1_scatter_gather.c -o b1
154
+ mpirun -np 4 ./b1
155
+ ```
156
+
157
+ Expect `Gathered squares: 1 4 9 16`. Remember scatter sends each rank one number, and gather returns the squares to rank 0.
158
+
159
+ ### B2 · Reduce and allreduce
160
+
161
+ ```bash
162
+ mpicc mpi/b2_reduce.c -o b2
163
+ mpirun -np 4 ./b2
164
+ ```
165
+
166
+ Expect MAX = 4, MIN = 1, SUM = 10, and PROD = 24. Remember reduce gives the result to rank 0, and allreduce gives it to every rank.
167
+
168
+ ### B3 · Send, receive, and broadcast
169
+
170
+ ```bash
171
+ mpicc mpi/b3_send_recv_bcast.c -o b3
172
+ mpirun -np 4 ./b3
173
+ ```
174
+
175
+ Expect rank 1 to receive 42 from rank 0, then every rank to report the broadcast value 100. Remember send and receive match on a tag, and every rank calls broadcast with the same root.
176
+
177
+ ### B4 · Three messages to the root
178
+
179
+ ```bash
180
+ mpicc mpi/b4_messages.c -o b4
181
+ mpirun -np 4 ./b4
182
+ ```
183
+
184
+ Expect rank 0 to print Bangalore, Ramaiah Institute of Technology, and Information Science and Engineering. Remember `strlen(message) + 1` sends the string terminator too.
185
+
186
+ ### B5 · Barrier and squares
187
+
188
+ ```bash
189
+ mpicc mpi/b5_barrier.c -o b5
190
+ mpirun -np 4 ./b5
191
+ ```
192
+
193
+ Expect squares 1, 4, 9, and 16. Each rank finishes its calculation before any rank passes the barrier. Lines printed by different ranks can appear in any order.
@@ -0,0 +1,201 @@
1
+ Metadata-Version: 2.4
2
+ Name: is71
3
+ Version: 0.1.0
4
+ Summary: C source and sample runs for the DC lab OpenMP and MPI exercises
5
+ Project-URL: Repository, https://github.com/atharvsgit/dc
6
+ Requires-Python: >=3.9
7
+ Description-Content-Type: text/markdown
8
+
9
+ # DC lab practical
10
+
11
+ ## Use the is71 command
12
+
13
+ Once version 0.1.0 is published to PyPI, install it in a Python 3.9+ virtual
14
+ environment.
15
+
16
+ ```bash
17
+ pip install is71
18
+ is71 src a1a
19
+ is71 src a4
20
+ is71 src b1 > b1.c
21
+ ```
22
+
23
+ Every program includes its compile and run commands, sample input, and
24
+ expected output. Use `is71 src --help` to list the exercise IDs. The programs
25
+ are bundled with the package, so the command works offline after installation.
26
+
27
+ ## Publish to PyPI
28
+
29
+ From the repository root, build and check the release files in a fresh
30
+ directory. The Vercel build uses `dist/` separately. On Ubuntu, install
31
+ `python3-venv` if Python reports that `ensurepip` is unavailable.
32
+
33
+ ```bash
34
+ python3 -m venv .venv
35
+ source .venv/bin/activate
36
+ python -m pip install build twine
37
+ release_dir=$(mktemp -d)
38
+ python -m build --outdir "$release_dir"
39
+ python -m twine check --strict "$release_dir"/*
40
+ ```
41
+
42
+ Create a PyPI API token with account-wide scope for the first upload, then run
43
+ the following and paste the token when Twine prompts for a password.
44
+
45
+ ```bash
46
+ python -m twine upload --username __token__ "$release_dir"/*
47
+ ```
48
+
49
+ For later releases, bump the version in `pyproject.toml` and use a
50
+ project-scoped token.
51
+
52
+ ## Download from Vercel
53
+
54
+ Each exercise has a short URL that returns the C source, followed by the
55
+ compile and run commands, sample input, and expected output in a C comment.
56
+ For example, `/a1a` opens Part A, question 1a.
57
+
58
+ ```bash
59
+ curl https://YOUR-SITE.vercel.app/a1a
60
+ ```
61
+
62
+ Add `-o a1a.c` to save the code, then follow the commands at the bottom of
63
+ the file. The homepage lists every route and shows a command with the current
64
+ hostname.
65
+
66
+ To deploy, import `atharvsgit/dc` in Vercel and use the repository root.
67
+ `vercel.json` sets the build command and output directory. Use the production
68
+ domain and allow unauthenticated access if you want curl to work without login.
69
+
70
+ ## Run from the repository
71
+
72
+ Open a terminal and enter the lab folder.
73
+
74
+ ```bash
75
+ cd "$HOME/Desktop/dc lab prac"
76
+ ```
77
+
78
+ ## Part A · OpenMP
79
+
80
+ Compile and run one pair of commands at a time. Each run below supplies sample input with `printf`. To type the input yourself, run the part of the line after `|`.
81
+
82
+ ### A1a · Parallel sum
83
+
84
+ ```bash
85
+ gcc -O2 -fopenmp openmp/a1a_sum.c -o a1a
86
+ printf '5\n1 2 3 4 5\n' | OMP_NUM_THREADS=4 ./a1a
87
+ ```
88
+
89
+ Expect `Sum = 15`. Remember `parallel for reduction(+:sum)`. The first number is the array size, with a limit of 100.
90
+
91
+ ### A1b · Loop scheduling
92
+
93
+ ```bash
94
+ gcc -O2 -fopenmp openmp/a1b_schedule.c -o a1b
95
+ printf '4\n' | OMP_DYNAMIC=FALSE OMP_NUM_THREADS=2 OMP_SCHEDULE=static,2 ./a1b
96
+ ```
97
+
98
+ Expect thread 0 to get iterations 0–1 and thread 1 to get 2–3. Try 9 iterations to see chunks assigned round-robin. Remember `schedule(runtime)` reads `OMP_SCHEDULE`.
99
+
100
+ ### A2 · Sequential and parallel merge sort
101
+
102
+ ```bash
103
+ gcc -O2 -fopenmp openmp/a2_merge_sort.c -o a2
104
+ printf '1000000\n' | OMP_DYNAMIC=FALSE ./a2
105
+ ```
106
+
107
+ The program generates the array from the size you enter and gives both runs identical data. Try 8 to see the sorted values. Remember `parallel sections` sorts two halves, then the main thread merges them.
108
+
109
+ ### A3a · Fibonacci with tasks
110
+
111
+ ```bash
112
+ gcc -O2 -fopenmp openmp/a3a_fibonacci.c -o a3a
113
+ printf '10\n' | OMP_NUM_THREADS=4 ./a3a
114
+ ```
115
+
116
+ Expect `0 1 1 2 3 5 8 13 21 34`. Remember `single` creates the tasks and `depend` makes each task wait for the two terms it reads. The program accepts up to 94 terms because it uses unsigned 64-bit arithmetic.
117
+
118
+ ### A3b · Parallel maximum
119
+
120
+ ```bash
121
+ gcc -O2 -fopenmp openmp/a3b_max.c -o a3b
122
+ printf '5\n-8 -2 -5 -9 -3\n' | OMP_NUM_THREADS=4 ./a3b
123
+ ```
124
+
125
+ Expect `Maximum = -2`. Remember `reduction(max:maximum)` with `INT_MIN` as the starting value. The first number is the array size, with a limit of 100.
126
+
127
+ ### A4 · Estimate π
128
+
129
+ ```bash
130
+ gcc -O2 -fopenmp openmp/a4_pi.c -o a4
131
+ printf '10000000\n' | OMP_NUM_THREADS=4 ./a4
132
+ ```
133
+
134
+ Expect both estimates to be near 3.14159265359. This uses the midpoint rule for the integral of `4 / (1 + x*x)` from 0 to 1. Remember `reduction(+:parallel_sum)` gives each thread a private accumulator and combines the results at the end.
135
+
136
+ ### A5 · Primes
137
+
138
+ ```bash
139
+ gcc -O2 -fopenmp openmp/a5_primes.c -o a5
140
+ printf '30\n' | OMP_NUM_THREADS=4 ./a5
141
+ printf '1000000\n' | OMP_NUM_THREADS=4 ./a5
142
+ ```
143
+
144
+ The small run lists 10 primes, from 2 through 29. The larger run reports 78,498 primes and omits the list. Remember each loop iteration tests one candidate independently.
145
+
146
+ For A2, A4, and A5, record the printed serial and parallel times in seconds. The reported difference is serial time minus parallel time. Timings vary across runs.
147
+
148
+ ## Part B · MPI
149
+
150
+ On Ubuntu, install the compiler wrapper and launcher if `command -v mpicc mpirun` cannot find them.
151
+
152
+ ```bash
153
+ sudo apt install openmpi-bin libopenmpi-dev
154
+ ```
155
+
156
+ Compile and run each program as follows. `-np 4` starts four processes on the local machine.
157
+
158
+ ### B1 · Scatter and gather
159
+
160
+ ```bash
161
+ mpicc mpi/b1_scatter_gather.c -o b1
162
+ mpirun -np 4 ./b1
163
+ ```
164
+
165
+ Expect `Gathered squares: 1 4 9 16`. Remember scatter sends each rank one number, and gather returns the squares to rank 0.
166
+
167
+ ### B2 · Reduce and allreduce
168
+
169
+ ```bash
170
+ mpicc mpi/b2_reduce.c -o b2
171
+ mpirun -np 4 ./b2
172
+ ```
173
+
174
+ Expect MAX = 4, MIN = 1, SUM = 10, and PROD = 24. Remember reduce gives the result to rank 0, and allreduce gives it to every rank.
175
+
176
+ ### B3 · Send, receive, and broadcast
177
+
178
+ ```bash
179
+ mpicc mpi/b3_send_recv_bcast.c -o b3
180
+ mpirun -np 4 ./b3
181
+ ```
182
+
183
+ Expect rank 1 to receive 42 from rank 0, then every rank to report the broadcast value 100. Remember send and receive match on a tag, and every rank calls broadcast with the same root.
184
+
185
+ ### B4 · Three messages to the root
186
+
187
+ ```bash
188
+ mpicc mpi/b4_messages.c -o b4
189
+ mpirun -np 4 ./b4
190
+ ```
191
+
192
+ Expect rank 0 to print Bangalore, Ramaiah Institute of Technology, and Information Science and Engineering. Remember `strlen(message) + 1` sends the string terminator too.
193
+
194
+ ### B5 · Barrier and squares
195
+
196
+ ```bash
197
+ mpicc mpi/b5_barrier.c -o b5
198
+ mpirun -np 4 ./b5
199
+ ```
200
+
201
+ Expect squares 1, 4, 9, and 16. Each rank finishes its calculation before any rank passes the barrier. Lines printed by different ranks can appear in any order.
@@ -0,0 +1,22 @@
1
+ README.md
2
+ pyproject.toml
3
+ is71.egg-info/PKG-INFO
4
+ is71.egg-info/SOURCES.txt
5
+ is71.egg-info/dependency_links.txt
6
+ is71.egg-info/entry_points.txt
7
+ is71.egg-info/top_level.txt
8
+ mpi/__init__.py
9
+ mpi/b1_scatter_gather.c
10
+ mpi/b2_reduce.c
11
+ mpi/b3_send_recv_bcast.c
12
+ mpi/b4_messages.c
13
+ mpi/b5_barrier.c
14
+ openmp/__init__.py
15
+ openmp/a1a_sum.c
16
+ openmp/a1b_schedule.c
17
+ openmp/a2_merge_sort.c
18
+ openmp/a3a_fibonacci.c
19
+ openmp/a3b_max.c
20
+ openmp/a4_pi.c
21
+ openmp/a5_primes.c
22
+ src/is71/__init__.py
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ is71 = is71:main
@@ -0,0 +1 @@
1
+ is71
@@ -0,0 +1 @@
1
+ """Bundled MPI exercises."""
@@ -0,0 +1,43 @@
1
+ #include <mpi.h>
2
+ #include <stdio.h>
3
+
4
+ int main(int argc, char *argv[])
5
+ {
6
+ int rank, size, value;
7
+ int numbers[4] = {1, 2, 3, 4};
8
+ int squares[4];
9
+
10
+ MPI_Init(&argc, &argv);
11
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank);
12
+ MPI_Comm_size(MPI_COMM_WORLD, &size);
13
+
14
+ if (size != 4) {
15
+ if (rank == 0)
16
+ puts("Run with exactly 4 processes.");
17
+ MPI_Finalize();
18
+ return 1;
19
+ }
20
+
21
+ MPI_Scatter(numbers, 1, MPI_INT, &value, 1, MPI_INT, 0, MPI_COMM_WORLD);
22
+ value *= value;
23
+ MPI_Gather(&value, 1, MPI_INT, squares, 1, MPI_INT, 0, MPI_COMM_WORLD);
24
+
25
+ if (rank == 0)
26
+ printf("Gathered squares: %d %d %d %d\n",
27
+ squares[0], squares[1], squares[2], squares[3]);
28
+
29
+ MPI_Finalize();
30
+ return 0;
31
+ }
32
+
33
+ /*
34
+ Compile and run (save this file as b1.c)
35
+ mpicc b1.c -o b1
36
+ mpirun -np 4 ./b1
37
+
38
+ Input
39
+ None. Rank 0 starts with 1, 2, 3, 4.
40
+
41
+ Expected result
42
+ Gathered squares: 1 4 9 16
43
+ */
@@ -0,0 +1,51 @@
1
+ #include <mpi.h>
2
+ #include <stdio.h>
3
+
4
+ int main(int argc, char *argv[])
5
+ {
6
+ int rank, size;
7
+ MPI_Op operations[] = {MPI_MAX, MPI_MIN, MPI_SUM, MPI_PROD};
8
+ const char *names[] = {"MAX", "MIN", "SUM", "PROD"};
9
+
10
+ MPI_Init(&argc, &argv);
11
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank);
12
+ MPI_Comm_size(MPI_COMM_WORLD, &size);
13
+
14
+ if (size != 4) {
15
+ if (rank == 0)
16
+ puts("Run with exactly 4 processes.");
17
+ MPI_Finalize();
18
+ return 1;
19
+ }
20
+
21
+ int value = rank + 1;
22
+ for (int i = 0; i < 4; i++) {
23
+ int root_result, all_result;
24
+
25
+ MPI_Reduce(&value, &root_result, 1, MPI_INT, operations[i], 0, MPI_COMM_WORLD);
26
+ MPI_Allreduce(&value, &all_result, 1, MPI_INT, operations[i], MPI_COMM_WORLD);
27
+
28
+ if (rank == 0)
29
+ printf("Reduce %s = %d\n", names[i], root_result);
30
+ printf("Rank %d: Allreduce %s = %d\n", rank, names[i], all_result);
31
+ }
32
+
33
+ MPI_Finalize();
34
+ return 0;
35
+ }
36
+
37
+ /*
38
+ Compile and run (save this file as b2.c)
39
+ mpicc b2.c -o b2
40
+ mpirun -np 4 ./b2
41
+
42
+ Input
43
+ None. Ranks 0 through 3 contribute 1, 2, 3, 4.
44
+
45
+ Expected result
46
+ Reduce MAX = 4
47
+ Reduce MIN = 1
48
+ Reduce SUM = 10
49
+ Reduce PROD = 24
50
+ Each rank also prints those four values for Allreduce. Line order can vary.
51
+ */
@@ -0,0 +1,50 @@
1
+ #include <mpi.h>
2
+ #include <stdio.h>
3
+
4
+ int main(int argc, char *argv[])
5
+ {
6
+ int rank, size, number = 42;
7
+
8
+ MPI_Init(&argc, &argv);
9
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank);
10
+ MPI_Comm_size(MPI_COMM_WORLD, &size);
11
+
12
+ if (size < 2) {
13
+ if (rank == 0)
14
+ puts("Run with at least 2 processes.");
15
+ MPI_Finalize();
16
+ return 1;
17
+ }
18
+
19
+ if (rank == 0) {
20
+ MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
21
+ } else if (rank == 1) {
22
+ MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
23
+ printf("Rank 1 received %d from rank 0\n", number);
24
+ }
25
+
26
+ int broadcast = 0;
27
+ if (rank == 0)
28
+ broadcast = 100;
29
+ MPI_Bcast(&broadcast, 1, MPI_INT, 0, MPI_COMM_WORLD);
30
+ printf("Rank %d has broadcast value %d\n", rank, broadcast);
31
+
32
+ MPI_Finalize();
33
+ return 0;
34
+ }
35
+
36
+ /*
37
+ Compile and run (save this file as b3.c)
38
+ mpicc b3.c -o b3
39
+ mpirun -np 4 ./b3
40
+
41
+ Input
42
+ None. Rank 0 sends 42 and broadcasts 100.
43
+
44
+ Expected result (line order can vary)
45
+ Rank 1 received 42 from rank 0
46
+ Rank 0 has broadcast value 100
47
+ Rank 1 has broadcast value 100
48
+ Rank 2 has broadcast value 100
49
+ Rank 3 has broadcast value 100
50
+ */
@@ -0,0 +1,54 @@
1
+ #include <mpi.h>
2
+ #include <stdio.h>
3
+ #include <string.h>
4
+
5
+ int main(int argc, char *argv[])
6
+ {
7
+ int rank, size;
8
+ const char *messages[] = {
9
+ "",
10
+ "Bangalore",
11
+ "Ramaiah Institute of Technology",
12
+ "Information Science and Engineering"
13
+ };
14
+
15
+ MPI_Init(&argc, &argv);
16
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank);
17
+ MPI_Comm_size(MPI_COMM_WORLD, &size);
18
+
19
+ if (size != 4) {
20
+ if (rank == 0)
21
+ puts("Run with exactly 4 processes.");
22
+ MPI_Finalize();
23
+ return 1;
24
+ }
25
+
26
+ if (rank == 0) {
27
+ for (int source = 1; source <= 3; source++) {
28
+ char message[64];
29
+ MPI_Recv(message, sizeof message, MPI_CHAR, source, 0,
30
+ MPI_COMM_WORLD, MPI_STATUS_IGNORE);
31
+ printf("Rank %d: %s\n", source, message);
32
+ }
33
+ } else {
34
+ MPI_Send(messages[rank], strlen(messages[rank]) + 1, MPI_CHAR,
35
+ 0, 0, MPI_COMM_WORLD);
36
+ }
37
+
38
+ MPI_Finalize();
39
+ return 0;
40
+ }
41
+
42
+ /*
43
+ Compile and run (save this file as b4.c)
44
+ mpicc b4.c -o b4
45
+ mpirun -np 4 ./b4
46
+
47
+ Input
48
+ None. Ranks 1 through 3 send strings built into the program.
49
+
50
+ Expected result
51
+ Rank 1: Bangalore
52
+ Rank 2: Ramaiah Institute of Technology
53
+ Rank 3: Information Science and Engineering
54
+ */
@@ -0,0 +1,42 @@
1
+ #include <mpi.h>
2
+ #include <stdio.h>
3
+
4
+ int main(int argc, char *argv[])
5
+ {
6
+ int rank, size;
7
+
8
+ MPI_Init(&argc, &argv);
9
+ MPI_Comm_rank(MPI_COMM_WORLD, &rank);
10
+ MPI_Comm_size(MPI_COMM_WORLD, &size);
11
+
12
+ if (size != 4) {
13
+ if (rank == 0)
14
+ puts("Run with exactly 4 processes.");
15
+ MPI_Finalize();
16
+ return 1;
17
+ }
18
+
19
+ int number = rank + 1;
20
+ int square = number * number;
21
+
22
+ MPI_Barrier(MPI_COMM_WORLD);
23
+ printf("Rank %d: %d squared = %d\n", rank, number, square);
24
+
25
+ MPI_Finalize();
26
+ return 0;
27
+ }
28
+
29
+ /*
30
+ Compile and run (save this file as b5.c)
31
+ mpicc b5.c -o b5
32
+ mpirun -np 4 ./b5
33
+
34
+ Input
35
+ None. Each rank squares its rank plus one.
36
+
37
+ Expected result (line order can vary)
38
+ Rank 0: 1 squared = 1
39
+ Rank 1: 2 squared = 4
40
+ Rank 2: 3 squared = 9
41
+ Rank 3: 4 squared = 16
42
+ */
@@ -0,0 +1 @@
1
+ """Bundled OpenMP exercises."""
@@ -0,0 +1,34 @@
1
+ #include <stdio.h>
2
+
3
+ int main(void)
4
+ {
5
+ int n, a[100];
6
+ long long sum = 0;
7
+
8
+ printf("Enter size (1-100), then the array elements: ");
9
+ if (scanf("%d", &n) != 1 || n < 1 || n > 100)
10
+ return 1;
11
+ for (int i = 0; i < n; i++)
12
+ if (scanf("%d", &a[i]) != 1)
13
+ return 1;
14
+
15
+ #pragma omp parallel for reduction(+:sum)
16
+ for (int i = 0; i < n; i++)
17
+ sum += a[i];
18
+
19
+ printf("Sum = %lld\n", sum);
20
+ return 0;
21
+ }
22
+
23
+ /*
24
+ Compile and run (save this file as a1a.c)
25
+ gcc -fopenmp a1a.c -o a1a
26
+ ./a1a
27
+
28
+ Input
29
+ 5
30
+ 1 2 3 4 5
31
+
32
+ Expected result
33
+ Sum = 15
34
+ */
@@ -0,0 +1,42 @@
1
+ #include <omp.h>
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+
5
+ int main(void)
6
+ {
7
+ int n;
8
+ printf("Enter number of iterations: ");
9
+ if (scanf("%d", &n) != 1 || n < 1)
10
+ return 1;
11
+
12
+ int *owner = malloc((size_t)n * sizeof *owner);
13
+ if (owner == NULL)
14
+ return 1;
15
+
16
+ // Set OMP_SCHEDULE=static,2 when running this program.
17
+ #pragma omp parallel for schedule(runtime)
18
+ for (int i = 0; i < n; i++)
19
+ owner[i] = omp_get_thread_num();
20
+
21
+ for (int i = 0; i < n;) {
22
+ int last = i + (i + 1 < n);
23
+ printf("Thread %d: Iterations %d -- %d\n", owner[i], i, last);
24
+ i = last + 1;
25
+ }
26
+
27
+ free(owner);
28
+ return 0;
29
+ }
30
+
31
+ /*
32
+ Compile and run (save this file as a1b.c)
33
+ gcc -fopenmp a1b.c -o a1b
34
+ OMP_DYNAMIC=FALSE OMP_NUM_THREADS=2 OMP_SCHEDULE=static,2 ./a1b
35
+
36
+ Input
37
+ 4
38
+
39
+ Expected result
40
+ Thread 0: Iterations 0 -- 1
41
+ Thread 1: Iterations 2 -- 3
42
+ */
@@ -0,0 +1,117 @@
1
+ #include <omp.h>
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+
6
+ // Merge the two sorted ranges [left, mid) and [mid, right).
7
+ static void merge(int a[], int temp[], int left, int mid, int right)
8
+ {
9
+ int i = left;
10
+ int j = mid;
11
+ int k = left;
12
+
13
+ while (i < mid && j < right) {
14
+ if (a[i] <= a[j])
15
+ temp[k++] = a[i++];
16
+ else
17
+ temp[k++] = a[j++];
18
+ }
19
+ while (i < mid)
20
+ temp[k++] = a[i++];
21
+ while (j < right)
22
+ temp[k++] = a[j++];
23
+
24
+ memcpy(a + left, temp + left, (size_t)(right - left) * sizeof *a);
25
+ }
26
+
27
+ static void merge_sort(int a[], int temp[], int left, int right)
28
+ {
29
+ if (right - left < 2)
30
+ return;
31
+
32
+ int mid = left + (right - left) / 2;
33
+ merge_sort(a, temp, left, mid);
34
+ merge_sort(a, temp, mid, right);
35
+ merge(a, temp, left, mid, right);
36
+ }
37
+
38
+ static void parallel_merge_sort(int a[], int temp[], int n)
39
+ {
40
+ if (n < 2)
41
+ return;
42
+
43
+ int mid = n / 2;
44
+ #pragma omp parallel sections num_threads(2)
45
+ {
46
+ #pragma omp section
47
+ merge_sort(a, temp, 0, mid);
48
+
49
+ #pragma omp section
50
+ merge_sort(a, temp, mid, n);
51
+ }
52
+ // The sections finish before the final merge starts.
53
+ merge(a, temp, 0, mid, n);
54
+ }
55
+
56
+ int main(void)
57
+ {
58
+ int n;
59
+ printf("Enter the number of elements: ");
60
+ if (scanf("%d", &n) != 1 || n < 1) {
61
+ fprintf(stderr, "Enter a positive array size.\n");
62
+ return 1;
63
+ }
64
+
65
+ size_t bytes = (size_t)n * sizeof(int);
66
+ int *serial = malloc(bytes);
67
+ int *parallel = malloc(bytes);
68
+ int *temp = malloc(bytes);
69
+ if (!serial || !parallel || !temp) {
70
+ fprintf(stderr, "Could not allocate the sorting arrays.\n");
71
+ free(serial);
72
+ free(parallel);
73
+ free(temp);
74
+ return 1;
75
+ }
76
+
77
+ for (int i = 0; i < n; ++i)
78
+ serial[i] = rand() % 10000;
79
+ memcpy(parallel, serial, bytes);
80
+
81
+ double start = omp_get_wtime();
82
+ merge_sort(serial, temp, 0, n);
83
+ double serial_time = omp_get_wtime() - start;
84
+
85
+ start = omp_get_wtime();
86
+ parallel_merge_sort(parallel, temp, n);
87
+ double parallel_time = omp_get_wtime() - start;
88
+
89
+ if (n <= 20) {
90
+ printf("Sorted values:");
91
+ for (int i = 0; i < n; ++i)
92
+ printf(" %d", parallel[i]);
93
+ putchar('\n');
94
+ }
95
+ int same = memcmp(serial, parallel, bytes) == 0;
96
+ printf("Outputs match: %s\n", same ? "yes" : "no");
97
+ printf("Serial: %.6f s\nParallel: %.6f s\nDifference: %+.6f s (serial - parallel)\n",
98
+ serial_time, parallel_time, serial_time - parallel_time);
99
+
100
+ free(serial);
101
+ free(parallel);
102
+ free(temp);
103
+ return same ? 0 : 1;
104
+ }
105
+
106
+ /*
107
+ Compile and run (save this file as a2.c)
108
+ gcc -O2 -fopenmp a2.c -o a2
109
+ ./a2
110
+
111
+ Input
112
+ 1000000
113
+
114
+ Expected result
115
+ Outputs match: yes
116
+ Serial, parallel, and difference times in seconds. Timings vary.
117
+ */
@@ -0,0 +1,44 @@
1
+ #include <omp.h>
2
+ #include <stdio.h>
3
+
4
+ int main(void)
5
+ {
6
+ int n;
7
+ printf("Enter the number of Fibonacci terms (1-94): ");
8
+ if (scanf("%d", &n) != 1 || n < 1 || n > 94) {
9
+ fprintf(stderr, "Enter a count between 1 and 94.\n");
10
+ return 1;
11
+ }
12
+
13
+ unsigned long long fib[94] = {0, 1};
14
+ #pragma omp parallel
15
+ {
16
+ #pragma omp single
17
+ {
18
+ for (int i = 2; i < n; ++i) {
19
+ // A term can be computed only after its two predecessors.
20
+ #pragma omp task firstprivate(i) shared(fib) depend(in: fib[i-2], fib[i-1]) depend(out: fib[i])
21
+ fib[i] = fib[i - 2] + fib[i - 1];
22
+ }
23
+ #pragma omp taskwait
24
+ }
25
+ }
26
+
27
+ printf("Fibonacci:");
28
+ for (int i = 0; i < n; ++i)
29
+ printf(" %llu", fib[i]);
30
+ putchar('\n');
31
+ return 0;
32
+ }
33
+
34
+ /*
35
+ Compile and run (save this file as a3a.c)
36
+ gcc -fopenmp a3a.c -o a3a
37
+ ./a3a
38
+
39
+ Input
40
+ 10
41
+
42
+ Expected result
43
+ Fibonacci: 0 1 1 2 3 5 8 13 21 34
44
+ */
@@ -0,0 +1,35 @@
1
+ #include <limits.h>
2
+ #include <stdio.h>
3
+
4
+ int main(void)
5
+ {
6
+ int n, a[100], maximum = INT_MIN;
7
+
8
+ printf("Enter size (1-100), then the array elements: ");
9
+ if (scanf("%d", &n) != 1 || n < 1 || n > 100)
10
+ return 1;
11
+ for (int i = 0; i < n; i++)
12
+ if (scanf("%d", &a[i]) != 1)
13
+ return 1;
14
+
15
+ #pragma omp parallel for reduction(max:maximum)
16
+ for (int i = 0; i < n; i++)
17
+ if (a[i] > maximum)
18
+ maximum = a[i];
19
+
20
+ printf("Maximum = %d\n", maximum);
21
+ return 0;
22
+ }
23
+
24
+ /*
25
+ Compile and run (save this file as a3b.c)
26
+ gcc -fopenmp a3b.c -o a3b
27
+ ./a3b
28
+
29
+ Input
30
+ 5
31
+ -8 -2 -5 -9 -3
32
+
33
+ Expected result
34
+ Maximum = -2
35
+ */
@@ -0,0 +1,51 @@
1
+ #include <omp.h>
2
+ #include <stdio.h>
3
+
4
+ int main(void)
5
+ {
6
+ int steps;
7
+ printf("Enter the number of integration steps: ");
8
+ if (scanf("%d", &steps) != 1 || steps < 1) {
9
+ fprintf(stderr, "Enter a positive step count.\n");
10
+ return 1;
11
+ }
12
+
13
+ // pi = integral of 4 / (1 + x*x) over [0, 1].
14
+ double width = 1.0 / steps;
15
+ double serial_sum = 0.0;
16
+ double start = omp_get_wtime();
17
+ for (int i = 0; i < steps; ++i) {
18
+ double x = (i + 0.5) * width;
19
+ serial_sum += 4.0 / (1.0 + x * x);
20
+ }
21
+ double serial_time = omp_get_wtime() - start;
22
+
23
+ // Each thread accumulates a private sum before OpenMP combines them.
24
+ double parallel_sum = 0.0;
25
+ start = omp_get_wtime();
26
+ #pragma omp parallel for reduction(+:parallel_sum)
27
+ for (int i = 0; i < steps; ++i) {
28
+ double x = (i + 0.5) * width;
29
+ parallel_sum += 4.0 / (1.0 + x * x);
30
+ }
31
+ double parallel_time = omp_get_wtime() - start;
32
+
33
+ printf("Serial pi: %.12f (%.6f s)\n", serial_sum * width, serial_time);
34
+ printf("Parallel pi: %.12f (%.6f s)\n", parallel_sum * width, parallel_time);
35
+ printf("Difference: %+.6f s (serial - parallel)\n", serial_time - parallel_time);
36
+ return 0;
37
+ }
38
+
39
+ /*
40
+ Compile and run (save this file as a4.c)
41
+ gcc -O2 -fopenmp a4.c -o a4
42
+ OMP_NUM_THREADS=4 ./a4
43
+
44
+ Input
45
+ 10000000
46
+
47
+ Expected result
48
+ Serial pi: about 3.14159265359
49
+ Parallel pi: about 3.14159265359
50
+ Serial, parallel, and difference times in seconds. Timings vary.
51
+ */
@@ -0,0 +1,83 @@
1
+ #include <omp.h>
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+
6
+ static int is_prime(int value)
7
+ {
8
+ if (value == 2)
9
+ return 1;
10
+ if (value < 2 || value % 2 == 0)
11
+ return 0;
12
+
13
+ for (int divisor = 3; divisor <= value / divisor; divisor += 2) {
14
+ if (value % divisor == 0)
15
+ return 0;
16
+ }
17
+ return 1;
18
+ }
19
+
20
+ int main(void)
21
+ {
22
+ int n;
23
+ printf("Find primes from 1 through: ");
24
+ if (scanf("%d", &n) != 1 || n < 1) {
25
+ fprintf(stderr, "Enter a positive upper bound.\n");
26
+ return 1;
27
+ }
28
+
29
+ unsigned char *serial = malloc((size_t)n);
30
+ unsigned char *parallel = malloc((size_t)n);
31
+ if (!serial || !parallel) {
32
+ fprintf(stderr, "Could not allocate the prime results.\n");
33
+ free(serial);
34
+ free(parallel);
35
+ return 1;
36
+ }
37
+
38
+ double start = omp_get_wtime();
39
+ for (int i = 0; i < n; ++i)
40
+ serial[i] = is_prime(i + 1);
41
+ double serial_time = omp_get_wtime() - start;
42
+
43
+ start = omp_get_wtime();
44
+ #pragma omp parallel for schedule(dynamic, 256)
45
+ for (int i = 0; i < n; ++i)
46
+ parallel[i] = is_prime(i + 1);
47
+ double parallel_time = omp_get_wtime() - start;
48
+
49
+ int correct = memcmp(serial, parallel, (size_t)n) == 0;
50
+ int count = 0;
51
+ for (int i = 0; i < n; ++i)
52
+ count += parallel[i];
53
+
54
+ if (n <= 100) {
55
+ printf("Primes:");
56
+ for (int i = 0; i < n; ++i)
57
+ if (parallel[i])
58
+ printf(" %d", i + 1);
59
+ putchar('\n');
60
+ }
61
+ printf("Prime count = %d\nRuns agree: %s\n", count, correct ? "yes" : "no");
62
+ printf("Serial: %.6f s\nParallel: %.6f s\nDifference: %+.6f s (serial - parallel)\n",
63
+ serial_time, parallel_time, serial_time - parallel_time);
64
+
65
+ free(serial);
66
+ free(parallel);
67
+ return correct ? 0 : 1;
68
+ }
69
+
70
+ /*
71
+ Compile and run (save this file as a5.c)
72
+ gcc -O2 -fopenmp a5.c -o a5
73
+ OMP_NUM_THREADS=4 ./a5
74
+
75
+ Input
76
+ 30
77
+
78
+ Expected result
79
+ Primes: 2 3 5 7 11 13 17 19 23 29
80
+ Prime count = 10
81
+ Runs agree: yes
82
+ Serial, parallel, and difference times in seconds. Timings vary.
83
+ */
@@ -0,0 +1,28 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "is71"
7
+ version = "0.1.0"
8
+ description = "C source and sample runs for the DC lab OpenMP and MPI exercises"
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+
12
+ [project.urls]
13
+ Repository = "https://github.com/atharvsgit/dc"
14
+
15
+ [project.scripts]
16
+ is71 = "is71:main"
17
+
18
+ [tool.setuptools]
19
+ packages = ["is71", "is71.openmp", "is71.mpi"]
20
+
21
+ [tool.setuptools.package-dir]
22
+ is71 = "src/is71"
23
+ "is71.openmp" = "openmp"
24
+ "is71.mpi" = "mpi"
25
+
26
+ [tool.setuptools.package-data]
27
+ "is71.openmp" = ["*.c"]
28
+ "is71.mpi" = ["*.c"]
is71-0.1.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,22 @@
1
+ """Print the C source and sample run for a DC lab exercise."""
2
+
3
+ import argparse
4
+ import sys
5
+ from importlib.resources import files
6
+
7
+
8
+ def main():
9
+ programs = {}
10
+ for package in ("is71.openmp", "is71.mpi"):
11
+ for source in files(package).iterdir():
12
+ if source.name.endswith(".c"):
13
+ route = source.name.split("_", 1)[0]
14
+ programs[route] = source
15
+
16
+ parser = argparse.ArgumentParser(prog="is71", description=__doc__)
17
+ commands = parser.add_subparsers(dest="command", required=True)
18
+ source = commands.add_parser("src", help="print a C program and its sample run")
19
+ source.add_argument("exercise", choices=sorted(programs))
20
+ args = parser.parse_args()
21
+
22
+ sys.stdout.buffer.write(programs[args.exercise].read_bytes())