Line Code
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
--- runcmsgrid.sh	2020-05-01 03:27:51.000000001 +0200
+++ runcmsgrid.sh	2020-05-02 10:46:24.000000001 +0200
@@ -48,1 +48,25 @@
+# test if the current file system allow setting folder permission to read-only.
+succ_setreadonly=true
+mkdir testpermit
+if fs listacl &>/dev/null; then
+    # AFS system detected. Use "fs sa" rather than "chmod" to set permission
+    echo "[MT] AFS system detected"
+    fs sa -dir testpermit -acl ${USER} read
+    if touch testpermit/newfile &>/dev/null; then succ_setreadonly=false; fi
+    fs sa -dir testpermit -acl ${USER} all
+else
+    chmod -w testpermit
+    if touch testpermit/newfile &>/dev/null; then succ_setreadonly=false; fi
+    chmod +w testpermit
+fi
+rm -r testpermit
+if [ $succ_setreadonly = false ]; then
+    echo "[MT] Warning: failed to set a folder to read-only mode with the current file system. Will use the normal mode and run with single core instead. Note that the script only works under directories in ordinary Unix file system or AFS system, while you are probably using other systems, e.g. EOS. This should NOT happen in a CRAB job. Please report the error if you see this in a CRAB job."
+fi
+
+if fs listacl &>/dev/null; then
+    fs sa -dir madevent -acl ${USER} all
+else
+    chmod +w madevent
+fi
 #make sure lhapdf points to local cmssw installation area
@@ -59,3 +83,15 @@
+function event_generate_per_thread () {
+
+# number of event to generate and seed in this thread
+thd=${1}
+nevt=${2}
+rnum=${3}
+
+if [ -d thread${thd} ]; then
+    rm -r thread${thd}
+fi
+mkdir thread${thd}
+cd thread${thd}
 #########################################
 # FORCE IT TO PRODUCE EXACTLY THE REQUIRED NUMBER OF EVENTS
 #########################################
@@ -98,1 +134,1 @@
-  ./run.sh $submitting_event $run_random_seed
+  ../process/run.sh $submitting_event $run_random_seed
@@ -124,3 +160,58 @@
+cd $LHEWORKDIR
+
+} ### end of function
+
+
+if [ $succ_setreadonly = false ] || [ $ncpu -eq 1 ] || [ $nevt -lt $ncpu ]; then
+    echo "[MT] Use normal mode and run on single core"
+    cd $LHEWORKDIR
+    event_generate_per_thread 0 $nevt $rnum
+    mv thread0/events.lhe.gz process/
+    rm -r thread0
+    cd process
+else
+    echo "[MT] Activate multi-threading for event generation -- will use $ncpu cores"
+    nevt_ave=$(( $nevt / $ncpu ))
+    for i in `seq 0 $(( $ncpu-2 ))`; do
+        nevt_per_thread[$i]=$nevt_ave
+    done
+    nevt_per_thread[$(( $ncpu-1 ))]=$(( $nevt - ($ncpu-1)*$nevt_ave ))
+
+    cd $LHEWORKDIR
+
+    # first do restore_data from the gridpack (necessary for the readonly mode)
+    cd process/madevent
+    ./bin/internal/restore_data default
+    cd ../..
+
+    # make the gridpack directory read-only to enable the multi-threading feature
+    if fs listacl &>/dev/null; then
+        fs sa -dir process/madevent -acl ${USER} read
+    else
+        chmod -w process/madevent
+    fi
+    
+    # when interrupt, resume write access and kill ALL multi-thread event generation commands
+    trap "cd $LHEWORKDIR; if fs listacl &>/dev/null; then fs sa -dir process/madevent -acl ${USER} all; else chmod +w process/madevent; fi; kill 0" SIGINT SIGTERM EXIT
+    for i in `seq 0 $(( $ncpu-1 ))`; do
+        event_generate_per_thread $i ${nevt_per_thread[$i]} $((rnum+10*$i)) | sed -e "s/^/[Thread $i] /" &
+    done; wait
+    trap - SIGINT SIGTERM EXIT # resume
+
+    if fs listacl &>/dev/null; then
+        fs sa -dir process/madevent -acl ${USER} all
+    else
+        chmod +w process/madevent
+    fi
+    cd process
+
+    # merge files produced in different threads
+    curl -s -L -o merge.pl https://raw.githubusercontent.com/cms-sw/genproductions/master/bin/MadGraph5_aMCatNLO/Utilities/merge.pl
+    chmod 755 merge.pl
+    ./merge.pl ../thread*/events.lhe.gz events.lhe.gz banner.txt
+    rm -r ../thread* banner.txt;
+fi
+
 #########################################
 #########################################
 #########################################