User Guide
local_to_assigned_visitor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #pragma once
9 
10 /**
11  * \file
12  * \brief \copybrief nmodl::visitor::LocalToAssignedVisitor
13  */
14 
15 #include "visitors/ast_visitor.hpp"
16 
17 namespace nmodl {
18 namespace visitor {
19 
20 /**
21  * @addtogroup visitor_classes
22  * @{
23  */
24 
25 /**
26  * \class LocalToAssignedVisitor
27  * \brief Visitor to convert top level LOCAL variables to ASSIGNED variables
28  *
29  * Some of the existing mod file include declaration of LOCAL variables in
30  * the top level of the mod file. Those variables are normally written in
31  * the INITIAL block which is executed potentially by multiple threads.
32  * This results into race condition in case of CoreNEURON. To avoid this,
33  * such variables are converted to ASSIGNED variables which by default are
34  * handled as RANGE.
35  *
36  * For example:
37  *
38  * \code{.mod}
39  * NEURON {
40  * SUFFIX test
41  * RANGE x
42  * }
43  *
44  * LOCAL qt
45  *
46  * INITIAL {
47  * qt = 10.0
48  * }
49  *
50  * PROCEDURE rates(v (mV)) {
51  * x = qt + 12.2
52  * }
53  * \endcode
54  *
55  * In the above example, `qt` is used as temporary variable to pass value from
56  * INITIAL block to PROCEDURE. This works fine in case of serial execution but
57  * in parallel execution we end up in race condition. To avoid this, we convert
58  * qt to ASSIGNED variable.
59  *
60  * \todo
61  * - Variables like qt are often constant. As long as INITIAL block is executed
62  * serially or qt is updated in atomic way then we don't have a problem.
63  */
64 
66  public:
67  /// \name Ctor & dtor
68  /// \{
69 
70  /// Default constructor
71  LocalToAssignedVisitor() = default;
72 
73  /// \}
74 
75  /// Visit ast::Program node to transform top level LOCAL variables
76  /// to ASSIGNED if they are written in the mod file
77  void visit_program(ast::Program& node) override;
78 };
79 
80 /** \} */ // end of visitor_classes
81 
82 } // namespace visitor
83 } // namespace nmodl
nmodl::visitor::LocalToAssignedVisitor::LocalToAssignedVisitor
LocalToAssignedVisitor()=default
Default constructor.
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::visitor::LocalToAssignedVisitor::visit_program
void visit_program(ast::Program &node) override
Visit ast::Program node to transform top level LOCAL variables to ASSIGNED if they are written in the...
Definition: local_to_assigned_visitor.cpp:21
nmodl::visitor::AstVisitor
Concrete visitor for all AST classes.
Definition: ast_visitor.hpp:37
nmodl::visitor::LocalToAssignedVisitor
Visitor to convert top level LOCAL variables to ASSIGNED variables.
Definition: local_to_assigned_visitor.hpp:65
nmodl::ast::Program
Represents top level AST node for whole NMODL input.
Definition: program.hpp:39
ast_visitor.hpp
Concrete visitor for all AST classes.