-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.py
988 lines (914 loc) · 35.9 KB
/
cluster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import logging
logger = logging.getLogger(__name__)
# ==============================================================================
import os, sys
import time
import inspect
# ==============================================================================
from tools_PKL.basic import (create_txt_from_L, get_path, ya, yapa, remove_dir,
dump_var, load_var, bash, bashr, get_Lrand, split_L_in_LL, dump_Lvar,
create_txt_from_s, fuse_LL, make_dir, load_Lvar, concat_Lfile, copy_file_in_dir,
get_s_from_object)
from tools_PKL.ssh import bash_on_remote
from tools_PKL.slurm import create_slurm_job
# ==============================================================================
#test2
def do_on_cluster(
f0=None,
s1_py=None,
user_local="[email protected]",
IP_local="192.168.11.160",
# ----------------------------
Dcluster=None,
user_cluster=None,
IP_cluster=None,
conda_env=None,
# ----
queue=None,
cmd_smina=None,
# ----------------------------
with_GNU_parallel=True,
ref=None,
level="INFO", # the logging level on the cluster
nLmin_by_job=200, # Number min of cases to treat by job
ncore_max=80, # number max of core to use
njob_max=1000,
dir_Lres="dir_Lres",
with_return_Lres=True,
with_fuse_LL=True, # to fuse the differents results of the parallelization
Lmodule_to_update=None,
):
# -----------------------------------------------
logger.info("do_on_cluster")
if Dcluster is not None:
user_cluster = Dcluster["user_cluster"]
IP_cluster = Dcluster["IP_cluster"]
conda_env = Dcluster["conda_env"]
cmd_smina = Dcluster["cmd_smina"]
queue = Dcluster["queue"]
if ref is None:
ref = f0.__name__
def decorator(func):
def wrapped(*args,s1_py = s1_py, **kwargs):
# --------------------------------------------------------------------
update_module_on_cluster(
Lmodule_to_update,
IP_cluster,
user_cluster,
)
func(*args, **kwargs)
# --------------------------------------------------------------------------
Lkw = list(kwargs.keys())
# --------------------------------------------------------------------------
path_cluster = f"/home/{user_cluster}"
path_local = get_path()
logger.info(f"* path_local={path_local}")
# to be sure that the same job is not still running
# --------------------------------------------------------------------------
cmd = f"scancel --user {user_cluster}--name {ref}"
bash_on_remote(
cmd,
user_remote=user_cluster,
IP_remote=IP_cluster,
)
# --------------------------------------------------------------------------
logger.info(f"Remove of directory {ref} on cluster ...")
Lcmd = [f"rm -rf {path_cluster}/{ref}"]
bash_on_remote(
Lcmd,
user_cluster,
IP_cluster,
)
# --------------------------------------------------------------------------
logger.info(f"Remove of directory of results in local")
remove_dir(dir_Lres)
# ========================================================================
# Creation of a new directory to gather all the informations
# usefull to launch the script on the cluster.
# --------------------------------------------------------------------------
logger.info(f"Creation of the directory to send")
make_dir(ref)
logger.info("copy of variables")
if "LTvar_to_copy" in Lkw:
LTvar_to_copy = kwargs["LTvar_to_copy"]
for T in LTvar_to_copy:
if type(T) is str:
logger.info(f"\t copy of {T}")
copy_file_in_dir(T, ref)
else:
logger.info(f"copy of {T[0]}")
dump_var(T[1], f"{ref}/{T[0]}")
os.chdir(ref)
make_dir(dir_Lres)
path_local_ref = get_path()
# --------------------------------------------------------------------------
LL = split_L_in_LL(
args[0],
nL=nLmin_by_job,
nLL_max=njob_max,
)
if len(LL) < ncore_max:
n_core = len(LL)
else:
n_core = ncore_max
Ls = dump_Lvar(LL, "L")
create_txt_from_L(Ls, "Ls.txt")
nLs = len(Ls)
create_txt_from_s(str(nLs), "nLs.txt")
# --------------------------------------------------------------------------
# Dump of informations that can't be pass directly
# --------------------------------------------------------------------------
# ========================================================================
# Creation of job_all.sh to launch of the different jobs
# can be sbatch or bash
# --------------------------------------------------------------------------
Ls = f"""
#!/bin/bash
sbatch --wait job1.sh
wait
""".split("\n")
create_txt_from_L(Ls[1:], "job_all.sh")
# ========================================================================
# Creation of job1.sh slurm file
# --------------------------------------------------------------------------
create_slurm_job(
"do1.py",
with_GNU_parallel=with_GNU_parallel,
ref=ref,
queue=queue,
fname="job1.sh",
n_core=n_core,
user_cluster=user_cluster,
conda_env=conda_env,
cmd_to_insert_in_0=f"cd /home/{user_cluster}/{ref}",
s_parallel="parallel1",
)
# --------------------------------------------------------------------------
# do1.py file
# --------------------------------------------------------------------------
# --------------------------------------------------------------------------
if f0:
module = f0.__module__
sf0 = f0.__name__
s1_py = ""
s_py = f"""
import sys
import os
import traceback
sys.path.insert(0, "/home/{user_cluster}/modules")
# =============================================================================
from tools_SBC.logging_SBC import (set_log, logger)
from tools_PKL.basic import (load_var, dump_var, get_line_n_in_file, move_file, touch,
remove_file, remove_Lfile)
# =============================================================================
from {module} import {sf0}
# ------------------------------------------------------------------------------
i_line = int(sys.argv[1])
file = get_line_n_in_file("Ls.txt", i_line)
Lx = load_var(file)
exec("Lx2 = [{sf0}(x) for x in Lx]")
dump_var(Lx2, f"{dir_Lres}/{{file}}")
remove_file(file)
"""
elif s1_py :
s_py = f"""
import sys
import os
import traceback
sys.path.insert(0, "/home/{user_cluster}/modules")
# ==============================================================================
from tools_PKL.basic import (load_var)
"""
Ls2 = []
if "LTvar_to_copy" in Lkw:
for T in LTvar_to_copy:
if "." not in T[0]:
Ls2.append(f"{T[0]} = load_var('{T[0]}')")
# -----------------------------------------------------------------------
Ls = s_py.split("\n")[1:] + Ls2 + s1_py.split("\n")[1:]
create_txt_from_L(Ls, "do1.py")
# ========================================================================
# Export of the directory on the cluster and launch of the job
# -------------------------------------------------------------------------
os.chdir("..")
logger.info(f"Transfert of {ref} on cluster ...")
bash(
f"scp -C -r {ref} {user_cluster}@{IP_cluster}:{path_cluster}",
info=True,
)
# --------------------------------------------------------------------------
# Launch of the jobs
# -------------------------------------------------------------------------
logger.info(f"Launch of jobs ...")
Lcmd = [f"cd {path_cluster}/{ref}",
"bash job_all.sh"]
bash_on_remote(
Lcmd,
user_cluster,
IP_cluster,
)
logger.info(f"on going ...")
logger.debug(f"wait for results")
start_time = time.time()
# --------------------------------------------------------------------------
logger.info(f"done in {round(time.time() - start_time)} s")
# --------------------------------------------------------------------------
# Loading of results
# -------------------------------------------------------------------------
logger.info(f"loading of the results")
os.chdir(path_local_ref)
remove_dir(dir_Lres)
bash(
f"scp -C -r {user_cluster}@{IP_cluster}:{path_cluster}/{ref}/{dir_Lres} {path_local_ref}",
info=True,
)
# ------------------------------------------------------------------------
Lcmd = [f"rm -rf {path_cluster}/{ref}"]
#bash_on_remote(Lcmd, user_cluster, IP_cluster)
if with_return_Lres:
os.chdir(dir_Lres)
LLres = load_Lvar("L*")
os.chdir(path_local)
if with_fuse_LL:
return fuse_LL(LLres)
else:
return LLres
os.chdir(path_local)
return wrapped
return decorator
def update_module_on_cluster(
project="tools_SBC",
IP_cluster=None,
user_cluster=None,
user_local="[email protected]",
):
if type(project) is not list:
Lproject = [project]
else:
Lproject = project
for project in Lproject:
cmd = (f"rsync -e ssh -az"
#f" --exclude-from=/home/{user_local}/Documents/Programmes/Gitlab/tools_SBC/rsync_exclude.txt"
f" --delete-after {project} {user_cluster}@{IP_cluster}:/home/{user_cluster}/modules")
bashr(cmd, info=True)
# =======================================================================================================================================================
def treat_on_cluster(
Lmol,
s1_py = "",
# ----------------------------
user_local="[email protected]",
IP_local="192.168.11.160",
# ----------------------------
Dcluster=None,
user_cluster="krezel",
IP_cluster="192.168.11.17",
cmd_smina="smina.static",
conda_env="SBC",
queue="cpu",
# ----------------------------
with_GNU_parallel=True,
ref="exemple",
level="INFO", # the logging level on the cluster
nLmin_by_job=200, # Number min of cases to treat by job
nmax_core=80, # number max of core to use
nmax_job=1000,
dir_Lres="dir_Lres",
):
logger.info("minimize_Lmol_with_smina_on_cluster")
# --------------------------------------------------------------------------
if Dcluster is not None:
user_cluster = Dcluster["user_cluster"]
IP_cluster = Dcluster["IP_cluster"]
conda_env = Dcluster["conda_env"]
cmd_smina = Dcluster["cmd_smina"]
queue = Dcluster["queue"]
# --------------------------------------------------------------------------
path_cluster = f"/home/{user_cluster}"
path_local = get_path()
logger.info(f"* path_local={path_local}")
# --------------------------------------------------------------------------
# to be sure that the same job is not still running
# --------------------------------------------------------------------------
cmd = f"scancel --user {user_cluster}--name {ref}"
bash_on_remote(
cmd,
user_remote=user_cluster,
IP_remote=IP_cluster,
)
# --------------------------------------------------------------------------
logger.info(f"Remove of directory {ref} on cluster ...")
Lcmd = [f"rm -rf {path_cluster}/{ref}"]
bash_on_remote(
Lcmd,
user_cluster,
IP_cluster,
)
# ========================================================================
logger.info("\t Creation of the repertories in local ...")
make_dir(ref)
os.chdir(ref)
path_local_ref = get_path()
make_dir(dir_Lres)
if type(Lmol) is str:
Lmol = load_Lmol_from_sdf(Lmol)
nLmol = len(Lmol)
logger.info(f"\t {nLmol} molecules to minimize")
if ".pdb" in protein:
protein = protein[:-4]
os.system(f"cp ../{protein}.pdb {protein}.pdb")
# -------------------------------------------------------------------------
LL0 = split_L_in_LL(
L0,
nL=nLmin_by_job,
nLL_max=nmax_job,
)
if len(LLmol) < nmax_core:
n_core = len(LLmol)
else:
n_core = nmax_core
Ls = dump_Lvar(LL0, "L")
create_txt_from_L(Ls, "Ls.txt")
nLs = len(Ls)
create_txt_from_s(str(nLs), "nLs.txt")
# ========================================================================
# Creation of job_all.sh to launch of the different jobs
# can be sbatch or bash
# --------------------------------------------------------------------------
Ls = f"""
#!/bin/bash
sbatch --wait job1.sh
wait
""".split("\n")
create_txt_from_L(Ls[1:], "job_all.sh")
# ========================================================================
# Creation of job1.sh slurm file
# --------------------------------------------------------------------------
create_slurm_job(
"do1.py",
with_GNU_parallel=with_GNU_parallel,
ref=ref,
queue=queue,
fname="job1.sh",
n_core=n_core,
#Lnode_to_exclude=Lnode_to_exclude,
user_cluster=user_cluster,
conda_env=conda_env,
cmd_to_insert_in_0=f"cd /home/{user_cluster}/{ref}",
s_parallel="parallel1",
)
# --------------------------------------------------------------------------
# minimize_Lmol_with_smina_on_cluster.py file
# --------------------------------------------------------------------------
Ls = s1_py.split("\n")
create_txt_from_L(Ls[1:], "do1.py")
# ========================================================================
# Export of the directory on the cluster and launch of the job
# -------------------------------------------------------------------------
os.chdir("..")
logger.info(f"Transfert of {ref} on cluster ...")
bash(
f"scp -C -r {ref} {user_cluster}@{IP_cluster}:{path_cluster}",
info=True,
)
# --------------------------------------------------------------------------
# Launch of the jobs
# -------------------------------------------------------------------------
logger.info(f"Launch of jobs ...")
Lcmd = [f"cd {path_cluster}/{ref}",
"bash job_all.sh"]
bash_on_remote(
Lcmd,
user_cluster,
IP_cluster,
)
logger.info(f"on going ...")
logger.debug(f"wait for results in {path_local_ref}")
start_time = time.time()
# --------------------------------------------------------------------------
logger.info(f"done in {round(time.time() - start_time)} s")
# --------------------------------------------------------------------------
# Loading of results
# -------------------------------------------------------------------------
logger.info(f"loading of the results")
bash(
f"scp -C -r {user_cluster}@{IP_cluster}:{path_cluster}/{ref}/{dir_Lres} {path_local}",
info=True,
)
# ------------------------------------------------------------------------
os.chdir(path_local)
Lcmd = [f"rm -rf {path_cluster}/{ref}"]
bash_on_remote(Lcmd, user_cluster, IP_cluster)
if return_Lmol:
os.chdir(dir_Lres)
return load_Lmol("Lmol*")
def get_n_job_on_cluster_core(
ssh_client=None,
user=None,
name=None
):
name2 = ""
if name is not None:
name2 = f"-n {name}"
cmd = f"squeue -u {user} {name2} -h -r | wc -l"
stdin, stdout, stderr = ssh_client.exec_command(cmd)
Ls = stdout.read().splitlines()
try:
n = int(Ls[0])
except:
n=-1
return n
def get_n_job_on_cluster(
ssh_client=None,
user=None,
name=None
):
"""
Gets the number of jobs still running or pending on the cluster
"""
if type(name) is list:
n = 0
Lname = name
for name in Lname:
n += get_n_job_on_cluster_core(
ssh_client=ssh_client,
user=user,
name=name
)
else:
n = get_n_job_on_cluster_core(
ssh_client=ssh_client,
user=user,
name=name
)
return n
def test_cluster_with_array_hello(
name="test2",
path_local=None,
path_cluster=None,
queue="cpu",
user_local="krezel@ICOA-03",
user="krezel",
IP_local="192.168.11.160",
IP_cluster="192.168.11.17",
ref="cluster_test",
conda_env="SBC", # Name of the environment.
):
"""
Simple test to verify that the cluster works.
A simple test.py is launched on the cluster that prints its path and prints "OK".
Info on ARRAY: https://crc.ku.edu/hpc/how-to/arrays
https://rcpedia.stanford.edu/topicGuides/jobArrayPythonExample.html
"""
logger.info("cluster_test1")
if path_cluster is None:
path_cluster = f"/home/{user}"
path_local = get_path()
# --------------------------------------------------------------------------
logger.info(f"\t Remove of directory {ref} on cluster ...")
cmd = f"ssh {user}@{IP_cluster} 'rm -r {path_cluster}/{ref}'"
logger.info(f"\t\t {cmd}")
os.system(cmd)
# --------------------------------------------------------------------------
# Creation of a new directory to gather all the informations
# usefull to launch the script on the cluster.
# --------------------------------------------------------------------------
remove_dir(ref)
os.makedirs(ref)
os.chdir(ref)
path_local_ref = get_path()
# --------------------------------------------------------------------------
# Creation of slurm file
# --------------------------------------------------------------------------
Ls = f"""
#!/bin/bash
#SBATCH -p {queue}
#SBATCH -J '{ref}'
#SBATCH --ntasks=1
#SBATCH --time=00:10:00
#SBATCH --array=1-10
#SBATCH -o hello-%j-%a.out
#SBATCH --error=test_%A_%a.err
source ~/.bashrc
conda activate SBC
cd /home/{user}/{ref}
srun python test.py $SLURM_ARRAY_TASK_ID
""".split("\n")
create_txt_from_L(Ls[1:], "job.sh")
# --------------------------------------------------------------------------
Ls = f"""
import sys
n = sys.argv[1]
print(f"Hello! I am a task number {{n}}")
""".split("\n")
create_txt_from_L(Ls[1:], "test.py")
# --------------------------------------------------------------------------
os.chdir(path_local)
# --------------------------------------------------------------------------
# Export of the directory on the cluster and launch of the job
# -------------------------------------------------------------------------
logger.info(f"\t Transfert of {ref} on cluster ...")
cmd = f"scp -r {ref} {user}@{IP_cluster}:{path_cluster}"
logger.info(f"\t\t {cmd}")
os.system(cmd)
logger.info("\t Launch on the cluster")
cmd = f"ssh {user}@{IP_cluster} 'cd {path_cluster}/{ref} ; sbatch job.sh'"
logger.info(f"\t\t {cmd}")
os.system(cmd)
def test_cluster_with_array_pickle(
name="test2",
path_local=None,
path_cluster=None,
queue="cpu",
user_local="krezel@ICOA-03",
user="krezel",
IP_local="192.168.11.160",
IP_cluster="192.168.11.17",
ref="cluster_test",
conda_env="SBC", # Name of the environment.
):
"""
Simple test to verify that the cluster works.
A simple test.py is launched on the cluster that prints its path and prints "OK".
Info on ARRAY: https://crc.ku.edu/hpc/how-to/arrays
"""
logger.info("cluster_test_with_array")
if path_cluster is None:
path_cluster = f"/home/{user}"
path_local = get_path()
logger.info(f"\t path_local={path_local}")
# --------------------------------------------------------------------------
logger.info(f"\t Remove of directory {ref} on cluster ...")
cmd = f"ssh {user}@{IP_cluster} 'rm -r {path_cluster}/{ref}'"
logger.info(f"\t\t {cmd}")
os.system(cmd)
# --------------------------------------------------------------------------
# Creation of a new directory to gather all the informations
# usefull to launch the script on the cluster.
# --------------------------------------------------------------------------
remove_dir(ref)
os.makedirs(ref)
os.chdir(ref)
path_local_ref = get_path()
# --------------------------------------------------------------------------
L = get_Lrand(nL=1000)
LL = split_L_in_LL(
L,
nL=21
)
Lfp = []
for i,L in enumerate(LL):
fp = f"L_{i}"
dump_var(L, fp)
Lfp.append(fp)
create_txt_from_L(Lfp, "Lfp.txt")
nLfp = len(Lfp)
# --------------------------------------------------------------------------
# Creation of slurm file
# --------------------------------------------------------------------------
Ls = f"""
#!/bin/bash
#SBATCH -p {queue}
#SBATCH -J '{ref}'
#SBATCH --no-requeue
#SBATCH --ntasks=1
#SBATCH --time=00:10:00
#SBATCH --array=1-{nLfp}%100
#SBATCH --output array-%a.out
#SBATCH --error array-%a.err
source ~/.bashrc
conda activate SBC
cd /home/{user}/{ref}
LINE=$(sed -n "$SLURM_ARRAY_TASK_ID"p Lfp.txt)
srun python test.py $LINE
scp L2* {user_local}@{IP_local}:{path_local_ref}
""".split("\n")
create_txt_from_L(Ls[1:], "job.sh")
# --------------------------------------------------------------------------job_sbatch
Ls = f"""
import os, sys
import numpy as np
import time
sys.path.insert(0, "/home/krezel/modules")
# =============================================================================
from tools_SBC.basic import load_var, dump_var, get_path
print("OK")
fp = str(sys.argv[1])
print(fp)
L = load_var(fp)
print(L)
L2 = [x*10 for x in L]
print(L2)
dump_var(L2, fp.replace("L", "L2"))
""".split("\n")
create_txt_from_L(Ls[1:], "test.py")
# --------------------------------------------------------------------------
os.chdir(path_local)
# --------------------------------------------------------------------------
# Export of the directory on the cluster and launch of the job
# -------------------------------------------------------------------------
logger.info(f"\t Transfert of {ref} on cluster ...")
cmd = f"scp -r {ref} {user}@{IP_cluster}:{path_cluster}"
logger.info(f"\t\t {cmd}")
os.system(cmd)
logger.info("\t Launch on the cluster")
cmd = f"ssh {user}@{IP_cluster} 'cd {path_cluster}/{ref} ; sbatch job.sh'"
logger.info(f"\t\t {cmd}")
os.system(cmd)
def test_cluster_with_array_txt(
name="test2",
path_local=None,
path_cluster=None,
queue="cpu",
user_local="krezel@ICOA-03",
user="krezel",
IP_local="192.168.11.160",
IP_cluster="192.168.11.17",
ref="cluster_test",
conda_env="SBC", # Name of the environment.
):
"""
Simple test to verify that the cluster works.
A simple test.py is launched on the cluster that prints its path and prints "OK".
Info on ARRAY: https://crc.ku.edu/hpc/how-to/arrays
"""
logger.info("cluster_test_with_array")
if path_cluster is None:
path_cluster = f"/home/{user}"
path_local = get_path()
logger.info(f"\t path_local={path_local}")
# --------------------------------------------------------------------------
logger.info(f"\t Remove of directory {ref} on cluster ...")
cmd = f"ssh {user}@{IP_cluster} 'rm -r {path_cluster}/{ref}'"
logger.info(f"\t\t {cmd}")
os.system(cmd)
# --------------------------------------------------------------------------
# Creation of a new directory to gather all the informations
# usefull to launch the script on the cluster.
# --------------------------------------------------------------------------
remove_dir(ref)
os.makedirs(ref)
os.chdir(ref)
path_local_ref = get_path()
# --------------------------------------------------------------------------
Lfile = []
for i in range(10):
file = f"H1_{i}.txt"
create_txt_from_s("Hello", file )
Lfile.append(file)
create_txt_from_L(Lfile, "Lfile.txt")
nLfile = len(Lfile)
# --------------------------------------------------------------------------
# Creation of slurm file
# --------------------------------------------------------------------------
Ls = f"""
#!/bin/bash
#SBATCH -p {queue}
#SBATCH -J '{ref}'
#SBATCH --no-requeue
#SBATCH --ntasks=1
#SBATCH --time=00:10:00
#SBATCH --array=1-{nLfile}%100
#SBATCH --output array-%a.out
#SBATCH --error array-%a.err
source ~/.bashrc
conda activate SBC
cd /home/{user}/{ref}
LINE=$(sed -n "$SLURM_ARRAY_TASK_ID"p Lfile.txt)
srun python test.py $LINE
scp H2* {user_local}@{IP_local}:{path_local_ref}
""".split("\n")
create_txt_from_L(Ls[1:], "job.sh")
# --------------------------------------------------------------------------
Ls = f"""
import os, sys
import numpy as np
import time
sys.path.insert(0, "/home/krezel/modules")
# =============================================================================
from tools_SBC.basic import (load_var, dump_var, get_path, create_txt_from_L,
load_Ls_from_txt)
file = str(sys.argv[1])
print(file)
Ls = load_Ls_from_txt(file)
print(Ls)
Ls.append("OK")
create_txt_from_L(Ls, file.replace("H1", "H2"))
""".split("\n")
create_txt_from_L(Ls[1:], "test.py")
# --------------------------------------------------------------------------
os.chdir(path_local)
# --------------------------------------------------------------------------
# Export of the directory on the cluster and launch of the job
# -------------------------------------------------------------------------
logger.info(f"\t Transfert of {ref} on cluster ...")
cmd = f"scp -r {ref} {user}@{IP_cluster}:{path_cluster}"
logger.info(f"\t\t {cmd}")
os.system(cmd)
logger.info("\t Launch on the cluster")
cmd = f"ssh {user}@{IP_cluster} 'cd {path_cluster}/{ref} ; sbatch job.sh'"
logger.info(f"\t\t {cmd}")
os.system(cmd)
def test_cluster_GPU_with_cupy(
name="test_GPU",
path_local=None,
path_cluster=None,
queue="rtx2080",
user_local="krezel@ICOA-03",
user="krezel",
IP_local="192.168.11.160",
IP_cluster="192.168.11.18",
ref="test_on_cluster_GPU",
conda_env="SBC", # Name of the environment.
n_core=4,
):
"""
Test if cupy works on the cluster of GPU
"""
logger.info("cluster_test_with_array")
if path_cluster is None:
path_cluster = f"/home/{user}"
path_local = get_path()
logger.info(f"\t path_local={path_local}")
# --------------------------------------------------------------------------
logger.info(f"\t Remove of directory {ref} on cluster ...")
cmd = f"ssh {user}@{IP_cluster} 'rm -r {path_cluster}/{ref}'"
logger.info(f"\t\t {cmd}")
os.system(cmd)
# --------------------------------------------------------------------------
# Creation of a new directory to gather all the informations
# usefull to launch the script on the cluster.
# --------------------------------------------------------------------------
remove_dir(ref)
os.makedirs(ref)
os.chdir(ref)
path_local_ref = get_path()
# --------------------------------------------------------------------------
create_txt_from_L(list(range(100)), "Ls.txt")
nLs = 100
# --------------------------------------------------------------------------
# Creation of slurm file
# --------------------------------------------------------------------------
Ls = f"""
#!/bin/bash
#SBATCH -C {queue}
#SBATCH -J '{ref}'
#SBATCH --no-requeue
#SBATCH --gres=gpu:GeForce:1
#SBATCH --time=00:10:00
#SBATCH --array=1-{nLs}%{n_core}
source ~/.bashrc
conda activate SBC
cd /home/{user}/{ref}
LINE=$(sed -n "$SLURM_ARRAY_TASK_ID"p Ls.txt)
srun python cluster_test_cupy_on_GPU.py $LINE
""".split("\n")
create_txt_from_L(Ls[1:], "job.sh")
# --------------------------------------------------------------------------
Ls = f"""
import os, sys
import cupy as cp
# -----------------------------
M = cp.random.rand(3000,3000)
cp.linalg.svd(M)
cp.cuda.Stream.null.synchronize()
print("done")
""".split("\n")
create_txt_from_L(Ls[1:], "cluster_test_cupy_on_GPU.py")
# --------------------------------------------------------------------------
os.chdir(path_local)
# --------------------------------------------------------------------------
# Export of the directory on the cluster and launch of the job
# -------------------------------------------------------------------------
logger.info(f"\t Transfert of {ref} on cluster ...")
cmd = f"scp -r {ref} {user}@{IP_cluster}:{path_cluster}"
logger.info(f"\t\t {cmd}")
os.system(cmd)
logger.info("\t Launch on the cluster")
cmd = f"ssh {user}@{IP_cluster} 'cd {path_cluster}/{ref} ; sbatch job.sh'"
logger.info(f"\t\t {cmd}")
os.system(cmd)
def test_cluster_GPU_with_numba(
name="test_GPU",
path_local=None,
path_cluster=None,
queue="rtx2080",
user_local="krezel@ICOA-03",
user="krezel",
IP_local="192.168.11.160",
IP_cluster="192.168.11.18",
ref="test_on_cluster_GPU",
conda_env="SBC", # Name of the environment.
n_core=4,
):
"""
Test if cupy works on the cluster of GPU
"""
logger.info("cluster_test_with_array")
if path_cluster is None:
path_cluster = f"/home/{user}"
path_local = get_path()
logger.info(f"\t path_local={path_local}")
# --------------------------------------------------------------------------
logger.info(f"\t Remove of directory {ref} on cluster ...")
cmd = f"ssh {user}@{IP_cluster} 'rm -r {path_cluster}/{ref}'"
logger.info(f"\t\t {cmd}")
os.system(cmd)
# --------------------------------------------------------------------------
# Creation of a new directory to gather all the informations
# usefull to launch the script on the cluster.
# --------------------------------------------------------------------------
remove_dir(ref)
os.makedirs(ref)
os.chdir(ref)
path_local_ref = get_path()
# --------------------------------------------------------------------------
create_txt_from_L(list(range(20)), "Ls.txt")
nLs = 20
# --------------------------------------------------------------------------
# Creation of slurm file
# --------------------------------------------------------------------------
Ls = f"""
#!/bin/bash
#SBATCH -C {queue}
#SBATCH -J '{ref}'
#SBATCH --no-requeue
#SBATCH --gres=gpu:GeForce:1
#SBATCH --time=00:10:00
#SBATCH --array=1-{nLs}%{n_core}
source ~/.bashrc
conda activate SBC
cd /home/{user}/{ref}
LINE=$(sed -n "$SLURM_ARRAY_TASK_ID"p Ls.txt)
srun python cluster_test_numba_on_GPU.py $LINE
""".split("\n")
create_txt_from_L(Ls[1:], "job.sh")
# --------------------------------------------------------------------------
# import numpy as np
# import numba
# from numba import cuda
# from time import perf_counter_ns
# # -----------------------------
# print(np.__version__)
# print(numba.__version__)
# cuda.detect()
# @cuda.jit
# def add_array(a,b,c):
# i = cuda.grid(1)
# if i < a.size:
# c[i] = a[i] + b[i]
# N = 1_000_000
# a = np.arange(N, dtype=np.)
# b = np.arange(N, dtype=np.float32)
# dev_a = cuda.to_device(a)
# dev_b = cuda.to_device(b)
# dev_c = cuda.device_array_like(dev_a)
# threads_per_block = 256
# blocks_per_grid = (N + (threads_per_block - 1)) // threads_per_block
# add_array[blocks_per_grid, threads_per_block](dev_a, dev_b, dev_c)
# c = dev_c.copy_to_host()
# print(np.allclose(a + b, c))
# # ----------------------------------------------------
# cuda.synchronize()
# timing = np.empty(101)
# for i in range(timing.size):
# tic = perf_counter_ns()
# add_array[blocks_per_grid, threads_per_block](dev_a, dev_b, dev_c)
# cuda.synchronize()
# toc = perf_counter_ns()
# timing[i] = toc - tic
# timing *= 1e-3 # convert to μs
# print(f"Elapsed time: {{timing.mean():.0f}} ± {{timing.std():.0f}} μs")
Ls = f"""
# ----------------------------------------------------
import math # Note that for the CUDA target, we need to use the scalar functions from the math module, not NumPy
import numpy as np
from numba import vectorize
SQRT_2PI = np.float32((2*math.pi)**0.5) # Precompute this constant as a float32. Numba will inline it at compile time.
@vectorize(['float32(float32, float32, float32)'], target='cuda')
def gaussian_pdf(x, mean, sigma):
'''Compute the value of a Gaussian probability density function at x with given mean and sigma.'''
return math.exp(-0.5 * ((x - mean) / sigma)**2) / (sigma * SQRT_2PI)
# Evaluate the Gaussian a million times!
x = np.random.uniform(-3, 3, size=1000000).astype(np.float32)
mean = np.float32(0.0)
sigma = np.float32(1.0)
# Quick test on a single element just to make sure it works
gaussian_pdf(x[0], mean, sigma)
print("done")
""".split("\n")
create_txt_from_L(Ls[1:], "cluster_test_numba_on_GPU.py")
# --------------------------------------------------------------------------
os.chdir(path_local)
# --------------------------------------------------------------------------
# Export of the directory on the cluster and launch of the job
# -------------------------------------------------------------------------
logger.info(f"\t Transfert of {ref} on cluster ...")
cmd = f"scp -r {ref} {user}@{IP_cluster}:{path_cluster}"
logger.info(f"\t\t {cmd}")
os.system(cmd)
logger.info("\t Launch on the cluster")
cmd = f"ssh {user}@{IP_cluster} 'cd {path_cluster}/{ref} ; sbatch job.sh'"
logger.info(f"\t\t {cmd}")
os.system(cmd)