Skip to content

Commit

Permalink
Merge pull request #16737 from bradcray/chpl_nodeID_nonvar
Browse files Browse the repository at this point in the history
Convert `chpl_nodeID` from `extern var` to `extern proc`

[reviewed by @mppf]

Declaring `chpl_nodeID` as a module-scope extern variable pointed to
an issue in which we weren't introducing a shadow variable for it as
we do for other module-scope variables.  However, it also shows a
weakness of using a module-scope extern variable for an external value
that is different on each locale: it suggests to Chapel that there's a
single extern variable when in fact there's a per-locale variable with
a distinct value on each locale.  This suggests that an extern var
isn't the best way to represent such values in Chapel.

In this PR, I change chpl_nodeID to an extern (paren-less) proc so
that the Chapel code which refers to it can remain unchanged; and I
assign it a new name in the C code so that we can implement it as a
static inline function without modifying other C code that refers to
the C-level variable.  The result is a minimal change to our code that
avoids the mismatch between describing such a value as a global
variable in Chapel that should result in a nearly identical
execution-time implementation.

Resolves #16716.
  • Loading branch information
bradcray committed Nov 19, 2020
2 parents 1e8866a + d7e8edd commit e27061c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 2 deletions.
3 changes: 2 additions & 1 deletion modules/internal/LocaleModelHelpSetup.chpl
Expand Up @@ -39,7 +39,8 @@ module LocaleModelHelpSetup {

config param debugLocaleModel = false;

extern var chpl_nodeID: chpl_nodeID_t;
pragma "fn synchronization free"
extern "get_chpl_nodeID" proc chpl_nodeID: chpl_nodeID_t;

record chpl_root_locale_accum {
var nPUsPhysAcc: atomic int;
Expand Down
8 changes: 8 additions & 0 deletions runtime/include/chpl-comm.h
Expand Up @@ -41,6 +41,14 @@ extern c_nodeid_t chpl_nodeID; // unique ID for each node: 0, 1, 2, ...
// the current task is running.
// Note also that this value is set only in chpl_comm_init to a value which is
// (hopefully) unique to the running image, and never changed again.

//
// Helper function for Chapel to get the value of chpl_nodeID
//
static inline c_nodeid_t get_chpl_nodeID(void) {
return chpl_nodeID;
}

extern int32_t chpl_numNodes; // number of nodes

size_t chpl_comm_getenvMaxHeapSize(void);
Expand Down
1 change: 0 additions & 1 deletion test/localeModels/dmk/locale_name.chpl
@@ -1,5 +1,4 @@
extern proc chpl_nodeName(): c_string;
extern var chpl_nodeID: chpl_nodeID_t;

var locale_name = here.chpl_name();

Expand Down
23 changes: 23 additions & 0 deletions test/localeModels/nodeIDbug.chpl
@@ -0,0 +1,23 @@
var s$: sync int = 0;

writeln("coforall: ");
coforall loc in Locales {
on loc {
while (s$.readFF() != here.id) { }
s$.readFE();
writeln("chpl_nodeID: ", chpl_nodeID, ", here.id = ", here.id);
s$.writeEF(here.id+1);
}
}
writeln();
s$.writeXF(0);

writeln("forall: ");
forall loc in Locales {
on loc {
while (s$.readFF() != here.id) { }
s$.readFE();
writeln("chpl_nodeID: ", chpl_nodeID, ", here.id = ", here.id);
s$.writeEF(here.id+1);
}
}
5 changes: 5 additions & 0 deletions test/localeModels/nodeIDbug.comm-none.good
@@ -0,0 +1,5 @@
coforall:
chpl_nodeID: 0, here.id = 0

forall:
chpl_nodeID: 0, here.id = 0
11 changes: 11 additions & 0 deletions test/localeModels/nodeIDbug.good
@@ -0,0 +1,11 @@
coforall:
chpl_nodeID: 0, here.id = 0
chpl_nodeID: 1, here.id = 1
chpl_nodeID: 2, here.id = 2
chpl_nodeID: 3, here.id = 3

forall:
chpl_nodeID: 0, here.id = 0
chpl_nodeID: 1, here.id = 1
chpl_nodeID: 2, here.id = 2
chpl_nodeID: 3, here.id = 3
1 change: 1 addition & 0 deletions test/localeModels/nodeIDbug.numlocales
@@ -0,0 +1 @@
4

0 comments on commit e27061c

Please sign in to comment.