Skip to content

Commit 5786e05

Browse files
TEST: Fix distributed examples not being executed (#2693)
* put daal4py distributed examples under 'main()' body * fix incorrect data paths
1 parent c98f6d4 commit 5786e05

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

examples/daal4py/linear_regression_spmd.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818
# run like this:
1919
# mpirun -n 4 python ./linreg_spmd.py
2020

21+
from pathlib import Path
22+
2123
from numpy import loadtxt
2224

2325
import daal4py as d4p
2426

25-
if __name__ == "__main__":
26-
# Initialize SPMD mode
27-
d4p.daalinit()
2827

28+
def main():
2929
# Each process gets its own data
30-
infile = (
31-
"./data/distributed/linear_regression_train_" + str(d4p.my_procid() + 1) + ".csv"
32-
)
30+
data_path = Path(__file__).parent / "data" / "distributed"
31+
infile = data_path / f"linear_regression_train_{d4p.my_procid() + 1}.csv"
3332

3433
# Configure a Linear regression training object
3534
train_algo = d4p.linear_regression_training(distributed=True)
@@ -47,7 +46,7 @@
4746
predict_algo = d4p.linear_regression_prediction()
4847
# read test data (with same #features)
4948
pdata = loadtxt(
50-
"./data/distributed/linear_regression_test.csv",
49+
data_path / "linear_regression_test.csv",
5150
delimiter=",",
5251
usecols=range(10),
5352
)
@@ -59,5 +58,10 @@
5958
# The prediction result provides prediction
6059
assert predict_result.prediction.shape == (pdata.shape[0], dep_data.shape[1])
6160

61+
62+
if __name__ == "__main__":
63+
# Initialize SPMD mode
64+
d4p.daalinit()
65+
main()
6266
print("All looks good!")
6367
d4p.daalfini()

examples/daal4py/naive_bayes_spmd.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@
2424

2525
import daal4py as d4p
2626

27-
if __name__ == "__main__":
28-
# Initialize SPMD mode
29-
d4p.daalinit()
3027

28+
def main():
3129
# Each process gets its own data
32-
data_path = Path(__file__).parent / "data" / "batch"
30+
data_path = Path(__file__).parent / "data" / "distributed"
3331
infile = data_path / "naivebayes_train_dense.csv"
3432

3533
# Configure a training object (20 classes)
@@ -47,14 +45,18 @@
4745
palgo = d4p.multinomial_naive_bayes_prediction(20)
4846
# read test data (with same #features)
4947
pdata = loadtxt(
50-
"./data/batch/naivebayes_test_dense.csv", delimiter=",", usecols=range(20)
48+
data_path / "naivebayes_test_dense.csv", delimiter=",", usecols=range(20)
5149
)
5250
# now predict using the model from the training above
5351
presult = palgo.compute(pdata, tresult.model)
5452

5553
# Prediction result provides prediction
5654
assert presult.prediction.shape == (pdata.shape[0], 1)
5755

58-
print("All looks good!")
5956

57+
if __name__ == "__main__":
58+
# Initialize SPMD mode
59+
d4p.daalinit()
60+
main()
61+
print("All looks good!")
6062
d4p.daalfini()

examples/daal4py/pca_spmd.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424

2525
import daal4py as d4p
2626

27-
if __name__ == "__main__":
28-
# Initialize SPMD mode
29-
d4p.daalinit()
3027

28+
def main():
3129
# Each process gets its own data
3230
data_path = Path(__file__).parent / "data" / "distributed"
3331
infile = data_path / f"pca_normalized_{d4p.my_procid() + 1}.csv"
@@ -55,5 +53,10 @@
5553
or allclose(result1.variances, result2.variances)
5654
)
5755

56+
57+
if __name__ == "__main__":
58+
# Initialize SPMD mode
59+
d4p.daalinit()
60+
main()
5861
print("All looks good!")
5962
d4p.daalfini()

examples/daal4py/ridge_regression_spmd.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@
1818
# run like this:
1919
# mpirun -n 4 python ./ridge_regression_spmd.py
2020

21+
from pathlib import Path
2122

2223
from numpy import loadtxt
2324

2425
import daal4py as d4p
2526

26-
if __name__ == "__main__":
27-
# Initialize SPMD mode
28-
d4p.daalinit()
2927

28+
def main():
3029
# Each process gets its own data
31-
infile = (
32-
"./data/distributed/linear_regression_train_" + str(d4p.my_procid() + 1) + ".csv"
33-
)
30+
data_path = Path(__file__).parent / "data" / "distributed"
31+
infile = data_path / f"linear_regression_train_{d4p.my_procid() + 1}.csv"
3432

3533
# Configure a Ridge regression training object
3634
train_algo = d4p.ridge_regression_training(distributed=True)
@@ -48,7 +46,7 @@
4846
predict_algo = d4p.ridge_regression_prediction()
4947
# read test data (with same #features)
5048
pdata = loadtxt(
51-
"./data/distributed/linear_regression_test.csv",
49+
data_path / "linear_regression_test.csv",
5250
delimiter=",",
5351
usecols=range(10),
5452
)
@@ -60,5 +58,10 @@
6058
# The prediction result provides prediction
6159
assert predict_result.prediction.shape == (pdata.shape[0], dep_data.shape[1])
6260

61+
62+
if __name__ == "__main__":
63+
# Initialize SPMD mode
64+
d4p.daalinit()
65+
main()
6366
print("All looks good!")
6467
d4p.daalfini()

tests/test_daal4py_spmd_examples.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ def test_dbscan_spmd(self):
183183
if "spmd" not in fname:
184184
# here we only run spmd examples
185185
continue
186+
if "spmd_chunks_read" in fname:
187+
continue
186188
stem = Path(fname).stem
187189
if stem in module_names_with_configs:
188190
continue

0 commit comments

Comments
 (0)