Skip to content

Commit b1d5098

Browse files
default to 1 interactive thread, or 0 if generating output
1 parent 2e6ffbc commit b1d5098

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/jloptions.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,13 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
623623
break;
624624
case 't': // threads
625625
errno = 0;
626-
jl_options.nthreadpools = 1;
627-
long nthreads = -1, nthreadsi = 0;
626+
jl_options.nthreadpools = 2;
627+
// By default:
628+
// default threads = -1 (== "auto")
629+
long nthreads = -1;
630+
// interactive threads = 1, or 0 if generating output
631+
long nthreadsi = jl_generating_output() ? 0 : 1;
632+
628633
if (!strncmp(optarg, "auto", 4)) {
629634
jl_options.nthreads = -1;
630635
if (optarg[4] == ',') {
@@ -633,10 +638,9 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
633638
else {
634639
errno = 0;
635640
nthreadsi = strtol(&optarg[5], &endptr, 10);
636-
if (errno != 0 || endptr == &optarg[5] || *endptr != 0 || nthreadsi < 1 || nthreadsi >= INT16_MAX)
637-
jl_errorf("julia: -t,--threads=auto,<m>; m must be an integer >= 1");
641+
if (errno != 0 || endptr == &optarg[5] || *endptr != 0 || nthreadsi < 0 || nthreadsi >= INT16_MAX)
642+
jl_errorf("julia: -t,--threads=auto,<m>; m must be an integer >= 0");
638643
}
639-
jl_options.nthreadpools++;
640644
}
641645
}
642646
else {
@@ -650,17 +654,18 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
650654
errno = 0;
651655
char *endptri;
652656
nthreadsi = strtol(&endptr[1], &endptri, 10);
653-
if (errno != 0 || endptri == &endptr[1] || *endptri != 0 || nthreadsi < 1 || nthreadsi >= INT16_MAX)
654-
jl_errorf("julia: -t,--threads=<n>,<m>; n and m must be integers >= 1");
657+
// Allow 0 for interactive
658+
if (errno != 0 || endptri == &endptr[1] || *endptri != 0 || nthreadsi < 0 || nthreadsi >= INT16_MAX)
659+
jl_errorf("julia: -t,--threads=<n>,<m>; m must be an integer ≥ 0");
660+
if (nthreadsi == 0)
661+
jl_options.nthreadpools = 1;
655662
}
656-
jl_options.nthreadpools++;
657663
}
658664
jl_options.nthreads = nthreads + nthreadsi;
659665
}
660666
int16_t *ntpp = (int16_t *)malloc_s(jl_options.nthreadpools * sizeof(int16_t));
661667
ntpp[0] = (int16_t)nthreads;
662-
if (jl_options.nthreadpools == 2)
663-
ntpp[1] = (int16_t)nthreadsi;
668+
ntpp[1] = (int16_t)nthreadsi;
664669
jl_options.nthreads_per_pool = ntpp;
665670
break;
666671
case 'p': // procs

src/threading.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -698,15 +698,15 @@ void jl_init_threading(void)
698698
// and `jl_n_threads_per_pool`.
699699
jl_n_threadpools = 2;
700700
int16_t nthreads = JULIA_NUM_THREADS;
701-
int16_t nthreadsi = 0;
701+
// if generating output default to 0 interactive threads, otherwise default to 1
702+
int16_t nthreadsi = jl_generating_output() ? 0 : 1;
702703
char *endptr, *endptri;
703704

704705
if (jl_options.nthreads != 0) { // --threads specified
705706
nthreads = jl_options.nthreads_per_pool[0];
706707
if (nthreads < 0)
707708
nthreads = jl_effective_threads();
708-
if (jl_options.nthreadpools == 2)
709-
nthreadsi = jl_options.nthreads_per_pool[1];
709+
nthreadsi = (jl_options.nthreadpools == 1) ? 0 : jl_options.nthreads_per_pool[1];
710710
}
711711
else if ((cp = getenv(NUM_THREADS_NAME))) { // ENV[NUM_THREADS_NAME] specified
712712
if (!strncmp(cp, "auto", 4)) {
@@ -722,13 +722,16 @@ void jl_init_threading(void)
722722
}
723723
if (*cp == ',') {
724724
cp++;
725-
if (!strncmp(cp, "auto", 4))
725+
if (!strncmp(cp, "auto", 4)) {
726726
nthreadsi = 1;
727+
cp += 4;
728+
}
727729
else {
728730
errno = 0;
729731
nthreadsi = strtol(cp, &endptri, 10);
730732
if (errno != 0 || endptri == cp || nthreadsi < 0)
731-
nthreadsi = 0;
733+
nthreadsi = 1;
734+
cp = endptri;
732735
}
733736
}
734737
}

0 commit comments

Comments
 (0)