Search code examples
valamsys2

MSYS2 Vala no console output


If I create a simple program consisting of one print then everything works fine and the output is there, but in this example code for some reason the output is empty. This is an example of optimization by vectorization:

void ink(int[] arr,int a){
    for (int i=0;i<arr.length-8;i+=8) {
        arr[i]+=a;  arr[i+1]+=a;    arr[i+2]+=a;    arr[i+3]+=a;
        arr[i+4]+=a;    arr[i+5]+=a;    arr[i+6]+=a;    arr[i+7]+=a;
        //print(@"$i , $(i+1), $(i+2), $(i+3)\n");
        //print(@"$(arr[i]) , $(arr[i+1]), $(arr[i+2]), $(arr[i+3])\n\n");
    }

    int idx = arr.length - (arr.length %8);
    for (int k = idx;k<arr.length;k++) {
        arr[k]+=a;
    }

// for (int i=0;i<arr.length;i++) {
//  arr[i]+=a;
// }
}

void printMass(int[] arr){
    stdout.printf("\nAfter ink\n");
foreach (int a in arr) {
    print(a.to_string() + "\n");
}
}

int main(string[] args) {
int arr [999999];

for (int i=0;i<arr.length;i++) {
    arr[i]=Random.int_range(0,100);
    //print(arr[i].to_string() + "\n");
}
stdout.printf(@"ink of $(arr.length) elements\n");
Timer tmr = new Timer();
ink(arr,5);
tmr.stop();
ulong mcSec=0;tmr.elapsed(out mcSec);
stdout.printf(@"microseconds have passed $mcSec");
stdout.printf("TEST");
//printMass(arr);
return 0;
}

On Linux i get:

ink of 999999 elements
microseconds have passed 2462TEST⏎ 

And on Windows MSYS2 console:

gavr@DESKTOP-B57MHT8 MINGW64 ~
$ ./console.exe

gavr@DESKTOP-B57MHT8 MINGW64 ~

Not even a regular print ("TEST"); I tried the standard example of a timer, and it works:

gavr@DESKTOP-B57MHT8 MINGW64 ~
$ ./timer.exe
1.1: 0.00014100000000000001, 141
1.2: 0.0011130000000000001, 1113
2.1: 0.00052999999999999998, 530

what's wrong with my code?


Solution

  • This might have something to do with terminal buffering with MSYS2. You could try stdout.flush (); at the end of the program.

    Another option is how Windows applications are compiled. There is the -mconsole compiler option to compile programs with console support. To pass that through from the Vala compiler you would use valac -X -mconsole myprogram.vala.