Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
M
MPI_ArrayTask
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tammam.alsoleman
MPI_ArrayTask
Commits
28e3521b
You need to sign in or sign up before continuing.
Commit
28e3521b
authored
Dec 25, 2025
by
tammam.alsoleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
create MPI_AVG class
parent
0249d7b8
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
7 deletions
+81
-7
MPI_AVG.java
src/main/java/org/example/MPI_AVG.java
+81
-0
Main.java
src/main/java/org/example/Main.java
+0
-7
No files found.
src/main/java/org/example/MPI_AVG.java
0 → 100644
View file @
28e3521b
package
org
.
example
;
import
mpi.*
;
import
java.util.Random
;
public
class
MPI_AVG
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
MPI
.
Init
(
args
);
int
rank
=
MPI
.
COMM_WORLD
.
Rank
();
int
size
=
MPI
.
COMM_WORLD
.
Size
();
int
totalElements
=
1000
;
// Calculate how many elements each process gets
// and handle the remainder for the last process
int
baseSize
=
totalElements
/
size
;
int
mySize
=
(
rank
==
size
-
1
)
?
(
totalElements
-
baseSize
*
(
size
-
1
))
:
baseSize
;
double
[]
fullMatrix
=
null
;
double
[]
localBuffer
=
new
double
[
mySize
];
// --- Task 1: Distribution ---
if
(
rank
==
0
)
{
fullMatrix
=
new
double
[
totalElements
];
Random
rand
=
new
Random
();
for
(
int
i
=
0
;
i
<
totalElements
;
i
++)
{
fullMatrix
[
i
]
=
rand
.
nextDouble
()
*
100
;
}
// Manually send data to handle potential unequal sizes (Scatterv-like logic)
int
offset
=
baseSize
;
for
(
int
i
=
1
;
i
<
size
;
i
++)
{
int
sendSize
=
(
i
==
size
-
1
)
?
(
totalElements
-
baseSize
*
(
size
-
1
))
:
baseSize
;
MPI
.
COMM_WORLD
.
Send
(
fullMatrix
,
offset
,
sendSize
,
MPI
.
DOUBLE
,
i
,
0
);
offset
+=
sendSize
;
}
// Rank 0 keeps its own part
System
.
arraycopy
(
fullMatrix
,
0
,
localBuffer
,
0
,
baseSize
);
}
else
{
// Workers receive their specific part
MPI
.
COMM_WORLD
.
Recv
(
localBuffer
,
0
,
mySize
,
MPI
.
DOUBLE
,
0
,
0
);
}
// Printing received elements
System
.
out
.
println
(
"Rank "
+
rank
+
" received "
+
mySize
+
" elements."
);
// --- Task 2: Local Average Calculation ---
double
localSum
=
0
;
for
(
double
val
:
localBuffer
)
{
localSum
+=
val
;
}
double
localAvg
=
localSum
/
mySize
;
System
.
out
.
println
(
"Rank "
+
rank
+
" Local Average: "
+
String
.
format
(
"%.2f"
,
localAvg
));
// --- Task 3: Global Average using Local Averages (Weighted Average) ---
// We need to gather all local averages and their counts to Rank 0
double
[]
allLocalAvgs
=
new
double
[
size
];
int
[]
allCounts
=
new
int
[
size
];
// Gather local averages into an array on Rank 0
MPI
.
COMM_WORLD
.
Gather
(
new
double
[]{
localAvg
},
0
,
1
,
MPI
.
DOUBLE
,
allLocalAvgs
,
0
,
1
,
MPI
.
DOUBLE
,
0
);
// Gather the counts (weights) into an array on Rank 0
MPI
.
COMM_WORLD
.
Gather
(
new
int
[]{
mySize
},
0
,
1
,
MPI
.
INT
,
allCounts
,
0
,
1
,
MPI
.
INT
,
0
);
if
(
rank
==
0
)
{
double
weightedSum
=
0
;
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
// Applying the weighted average formula: Sum(Avg_i * Count_i)
weightedSum
+=
(
allLocalAvgs
[
i
]
*
allCounts
[
i
]);
}
double
globalAverage
=
weightedSum
/
totalElements
;
System
.
out
.
println
(
"\n--- FINAL CALCULATION (Weighted Mean) ---"
);
System
.
out
.
println
(
"Global Average derived from local averages: "
+
String
.
format
(
"%.4f"
,
globalAverage
));
System
.
out
.
println
(
"========================================\n"
);
}
MPI
.
Finalize
();
}
}
\ No newline at end of file
src/main/java/org/example/Main.java
deleted
100644 → 0
View file @
0249d7b8
package
org
.
example
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment