problema con dos bucles anidados en bash

I am trying to loop over two series of variables and make them with R statistical software to get the correlation results.

I got confused why the bigger loop (on mica_headers) doesn't take place while the inner loop finishes each time.

#!/bin/sh
#set -e


micaHeaderList="tot_instruction ILP32 ILP64 ILP128 ILP256 total_ins_count_for_hpc_alignment totInstruction mem-read mem-write control-flow arithmetic floating-point stack shift string sse other nop InstrFootprint64 InstrFootprint4k DataFootprint64 DataFootprint4k mem_access memReuseDist0-2 memReuseDist2-4 memReuseDist4-8 memReuseDist8-16 memReuseDist16-32 memReuseDist32-64 memReuseDist64-128 memReuseDist128-256 memReuseDist256-512 memReuseDist512-1k memReuseDist1k-2k memReuseDist2k-4k memReuseDist4k-8k memReuseDist8k-16k memReuseDist16k-32k memReuseDist32k-64k memReuseDist64k-128k memReuseDist128k-256k memReuseDist256k-512k memReuseDist512k-00 GAg_mispred_cnt_4bits PAg_mispred_cnt_4bits GAs_mispred_cnt_4bits PAs_mispred_cnt_4bits GAg_mispred_cnt_8bits PAg_mispred_cnt_8bits GAs_mispred_cnt_8bits PAs_mispred_cnt_8bits GAg_mispred_cnt_12bits PAg_mispred_cnt_12bits GAs_mispred_cnt_12bits PAs_mispred_cnt_12bits total_brCount total_transactionCount total_takenCount total_num_ops instr_reg_cnt total_reg_use_cnt total_reg_age reg_age_cnt_1 reg_age_cnt_2 reg_age_cnt_4 reg_age_cnt_8 reg_age_cnt_16 reg_age_cnt_32 reg_age_cnt_64 mem_read_cnt mem_read_local_stride_0 mem_read_local_stride_8 mem_read_local_stride_64 mem_read_local_stride_512 mem_read_local_stride_4096 mem_read_local_stride_32768 mem_read_local_stride_262144 mem_read_global_stride_0 mem_read_global_stride_8 mem_read_global_stride_64 mem_read_global_stride_512 mem_read_global_stride_4096 mem_read_global_stride_32768 mem_read_global_stride_262144 mem_write_cnt mem_write_local_stride_0 mem_write_local_stride_8 mem_write_local_stride_64 mem_write_local_stride_512 mem_write_local_stride_4096 mem_write_local_stride_32768 mem_write_local_stride_262144 mem_write_global_stride_0 mem_write_global_stride_8 mem_write_global_stride_64 mem_write_global_stride_512 mem_write_global_stride_4096 mem_write_global_stride_32768 mem_write_global_stride_262144"
mhToBeReplaced="ILP32"


compilerOptionList="funsafe_math_optimizations fno_guess_branch_probability fno_ivopts fno_tree_loop_optimize fno_inline_functions funroll_all_loops fno_omit_frame_pointer falign_jumps fselective_scheduling fno_inline_small_functions fno_tree_pre ftracer fno_move_loop_invariants"
coToBeReplaced="fno_guess_branch_probability"

for mica_header in $micaHeaderList
do

    for compiler_option in $compilerOptionList
    do

        echo "Calculating $compiler_option correlation for $mica_header"
        sed -i "s/$coToBeReplaced/$compiler_option/g" r.scr
        coToBeReplaced=$compiler_option
        make
    done


    sed -i "s/$mhToBeReplaced/$mica_header/g" r.scr
    mhToBeReplaced=$mica_header

done

Edited: Generally, in order to avoid inconsistency between the values of iterations and source file, how could I be able to link these two together from different files. i.e. micaHeaderList with the headers inside the ALL.scv file ?

preguntado el 09 de septiembre de 13 a las 22:09

There's no obvious reason for the problem. Have you tried creating an SSCCE (Ejemplo breve, autónomo y correcto) with, for example, vastly reduced lists for micaHeaderList y compilerOptionList? -

yes @JonathanLeffler, I tested it with 3 strings each but only the compiler_option list was looping, so instead of 9 diverse run I had like 3 duplication of one mica_header. By the way, both sed -i commands are working itself in bash -

If you run your reduced case of 3 strings each, what do you see if you echo $mica_header en el capítulo respecto a la dentro for loop and in the afuera for loop? You would expect to see each mica_header string echoed 3 times, but it would be interesting to know where it goes wrong. -

Estás intentando make inside the inner loop with the same (modified) source file. Then you replace but don't make in the outer loop after the make, so the final round of mh replacements are not going to be implemented. What are the files produced by your make operation? Do you want to do a make clean or move the compiled files somewhere else after each iteration? -

that make is to invoke the makefile of R software @beroe. I am goona check with my coleague about the consistency of the mica_headers cuz the only leakage could be there -

1 Respuestas

In the script there is a mistake which echoes a value of $mica_header which will be substituted only for make calls in the next outer loop run. This is caused by the second sed being run only after make. It should have been called before the inner loop. Could not this cause the confusion?

Also instead of replacing one value of a parameter for another it is better to create a template of r.src file with placeholders for the parameters to be changed. This way we can be sure that for some values of the parameters there could not be conflicts with the search patterns and that a typo will not prevent substitution.

Below is an improved script with considerably simpler code. The part inside if test "$Test" = 1 is just a testing code activated by Test=1. For this script just create a template for r.scr llamado r.scr.template and put the placeholders %%MicaHeader%% y %%CompilerOption%% instead of the parameters which should be replaced. For the first test there is a shorter list of parameters.

#!/bin/sh

micaHeaderList="tot_instruction ILP32 ILP64"
mhToBeReplaced=%%MicaHeader%%

compilerOptionList="funsafe_math_optimizations fno_guess_branch_probability fno_ivopts fno_tree_loop_optimize"
coToBeReplaced=%%CompilerOption%%

TemplateFile=r.scr.template
OutputFile=r.scr
Test=1

if test "$Test" = 1
then
    TemplateFile="${TemplateFile}-test"
    echo "$coToBeReplaced $mhToBeReplaced" >"$TemplateFile"
    make ()
    {
        echo -n ">>> make: "
        cat "$OutputFile"
    }
fi

for mica_header in $micaHeaderList
do
    for compiler_option in $compilerOptionList
    do
        echo "Calculating $compiler_option correlation for $mica_header"
        sed "s/$mhToBeReplaced/$mica_header/g;s/$coToBeReplaced/$compiler_option/g" "$TemplateFile" >"$OutputFile"
        make
    done
done

If the problem still persists:

  1. Check the generated r.src archivos.
  2. Verifica si el make script really uses the substituted parameters from r.src.

Respondido el 10 de Septiembre de 13 a las 08:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.