[boost] Create Graph in Function
Perhaps an easy answer, but I am working on adding a boost graph to an
existing code. I would like to create the graph in a function (not main) and
then read/modify it in a separate function. I am unsure as to what I need to
pass between the functions to allow this to work.
In other words, I am able to correctly create the graph in a function and
modify as necessary. This function will be called many times as the code
runs and it is inefficient to recreate the graph every time. I want to
create the graph once when the code is run and pass the created graph to the
function.
Due to the structure of the code (mostly Fortran), I can not define the
graph in the main function of the code.
My graph is defined as:
adjacency_list
On Wed, Oct 28, 2009 at 6:12 PM, James Thunes
Perhaps an easy answer, but I am working on adding a boost graph to an existing code. I would like to create the graph in a function (not main) and then read/modify it in a separate function. I am unsure as to what I need to pass between the functions to allow this to work.
In other words, I am able to correctly create the graph in a function and modify as necessary. This function will be called many times as the code runs and it is inefficient to recreate the graph every time. I want to create the graph once when the code is run and pass the created graph to the function.
Due to the structure of the code (mostly Fortran), I can not define the graph in the main function of the code.
My graph is defined as: adjacency_list
Graph; Thank you for any help you can give, James
Try shared_ptr. Something like.
function(){
typedef adjacency_list
On Wed, 28 Oct 2009, Sandeep Gupta wrote:
On Wed, Oct 28, 2009 at 6:12 PM, James Thunes
wrote: Perhaps an easy answer, but I am working on adding a boost graph to an existing code. I would like to create the graph in a function (not main) and then read/modify it in a separate function. I am unsure as to what I need to pass between the functions to allow this to work. In other words, I am able to correctly create the graph in a function and modify as necessary. This function will be called many times as the code runs and it is inefficient to recreate the graph every time. I want to create the graph once when the code is run and pass the created graph to the function.
Due to the structure of the code (mostly Fortran), I can not define the graph in the main function of the code.
My graph is defined as: adjacency_list
Graph; Thank you for any help you can give, James
Try shared_ptr. Something like.
function(){ typedef adjacency_list
Graph; shared_ptr< Graph > gptr( new Graph(n) ); return gptr; }
You could also just declare the graph in whichever scope will end up storing it and then pass it by reference to the construction function: void function(Graph& g) { // build g, or build a temporary and assign it to g } Graph the_graph; function(the_graph); Where are you going to be putting the final graph? Will it be in a variable in your main(), even if it is not constructed there? -- Jeremiah Willcock
Jeremiah Willcock wrote:
On Wed, 28 Oct 2009, Sandeep Gupta wrote:
On Wed, Oct 28, 2009 at 6:12 PM, James Thunes
wrote: Perhaps an easy answer, but I am working on adding a boost graph to an existing code. I would like to create the graph in a function (not main) and then read/modify it in a separate function. I am unsure as to what I need to pass between the functions to allow this to work. In other words, I am able to correctly create the graph in a function and modify as necessary. This function will be called many times as the code runs and it is inefficient to recreate the graph every time. I want to create the graph once when the code is run and pass the created graph to the function.
Due to the structure of the code (mostly Fortran), I can not define the graph in the main function of the code.
My graph is defined as: adjacency_list
Graph; Thank you for any help you can give, James
Try shared_ptr. Something like.
function(){ typedef adjacency_list
Graph; shared_ptr< Graph > gptr( new Graph(n) ); return gptr; } You could also just declare the graph in whichever scope will end up storing it and then pass it by reference to the construction function:
void function(Graph& g) { // build g, or build a temporary and assign it to g }
Graph the_graph; function(the_graph);
Where are you going to be putting the final graph? Will it be in a variable in your main(), even if it is not constructed there?
-- Jeremiah Willcock Jeremiah,
I'm sorry if I was not clear in my first post. The majority of the code (along with the main routine) is written in Fortran. I am using the graph to update a number of arrays which are needed for this Fortran code. I will create the graph in a subroutine (or c++ function) and modify it in another c++ function. The graph is not called in the main routine, only in subroutines. I need to know the way the graph is stored so I can pass it back to the main function (or perhaps define it as a public variable). In reference to the code snippet above, am I correct in reading it as the creating routine will be defined as: void function(Graph& g) { // build g, or build a temporary and assign it to g } and then the function to modify it would be defined as: void modify_the_graph(Graph& g){ \\ some graph manipulations here } -James Thunes
On Wed, 28 Oct 2009, James wrote:
Jeremiah Willcock wrote:
On Wed, 28 Oct 2009, Sandeep Gupta wrote:
On Wed, Oct 28, 2009 at 6:12 PM, James Thunes
wrote: Perhaps an easy answer, but I am working on adding a boost graph to an existing code. I would like to create the graph in a function (not main) and then read/modify it in a separate function. I am unsure as to what I need to pass between the functions to allow this to work. In other words, I am able to correctly create the graph in a function and modify as necessary. This function will be called many times as the code runs and it is inefficient to recreate the graph every time. I want to create the graph once when the code is run and pass the created graph to the function.
Due to the structure of the code (mostly Fortran), I can not define the graph in the main function of the code.
My graph is defined as: adjacency_list
Graph; Thank you for any help you can give, James
Try shared_ptr. Something like.
function(){ typedef adjacency_list
Graph; shared_ptr< Graph > gptr( new Graph(n) ); return gptr; } You could also just declare the graph in whichever scope will end up storing it and then pass it by reference to the construction function:
void function(Graph& g) { // build g, or build a temporary and assign it to g }
Graph the_graph; function(the_graph);
Where are you going to be putting the final graph? Will it be in a variable in your main(), even if it is not constructed there?
-- Jeremiah Willcock Jeremiah,
I'm sorry if I was not clear in my first post. The majority of the code (along with the main routine) is written in Fortran. I am using the graph to update a number of arrays which are needed for this Fortran code. I will create the graph in a subroutine (or c++ function) and modify it in another c++ function. The graph is not called in the main routine, only in subroutines.
I need to know the way the graph is stored so I can pass it back to the main function (or perhaps define it as a public variable).
In reference to the code snippet above, am I correct in reading it as the creating routine will be defined as: void function(Graph& g) { // build g, or build a temporary and assign it to g } and then the function to modify it would be defined as: void modify_the_graph(Graph& g){ \\ some graph manipulations here }
Where is the variable holding the graph going to be? If it doesn't have a Fortran type the variable cannot be in Fortran code. Are you going to have a global variable (in C++) that contains the graph? Otherwise, you might need to use a raw (not shared) pointer to the graph, managing the memory yourself, and pass it between Fortran and C++ as either some kind of interoperability C pointer type (in newer Fortran versions), a large enough integer, or maybe a Cray pointer. -- Jeremiah Willcock
Jeremiah Willcock wrote:
On Wed, 28 Oct 2009, James wrote:
Jeremiah Willcock wrote:
You could also just declare the graph in whichever scope will end up storing it and then pass it by reference to the construction function:
void function(Graph& g) { // build g, or build a temporary and assign it to g }
Graph the_graph; function(the_graph);
Where are you going to be putting the final graph? Will it be in a variable in your main(), even if it is not constructed there?
-- Jeremiah Willcock Jeremiah,
I'm sorry if I was not clear in my first post. The majority of the code (along with the main routine) is written in Fortran. I am using the graph to update a number of arrays which are needed for this Fortran code. I will create the graph in a subroutine (or c++ function) and modify it in another c++ function. The graph is not called in the main routine, only in subroutines.
I need to know the way the graph is stored so I can pass it back to the main function (or perhaps define it as a public variable).
In reference to the code snippet above, am I correct in reading it as the creating routine will be defined as: void function(Graph& g) { // build g, or build a temporary and assign it to g } and then the function to modify it would be defined as: void modify_the_graph(Graph& g){ \\ some graph manipulations here }
Where is the variable holding the graph going to be? If it doesn't have a Fortran type the variable cannot be in Fortran code. Are you going to have a global variable (in C++) that contains the graph? Otherwise, you might need to use a raw (not shared) pointer to the graph, managing the memory yourself, and pass it between Fortran and C++ as either some kind of interoperability C pointer type (in newer Fortran versions), a large enough integer, or maybe a Cray pointer.
-- Jeremiah Willcock
That is the issue. I agree that to manipulate the graph in the Fortran portion of the code would be difficult (due the lack of a comparable data type). Luckily, I don't need to modify the graph in the Fortran portion of the code, I just need to allow for a c++ function to access the graph. If I can define the graph as a global variable in c++ and negate the need to pass the graph from the create_graph function to the modify_graph function via the Fortran main routine: Create_Graph.cpp -> Main.f -> Modify_Graph.cpp would be great. Would I accomplish this as: in create_graph.cpp Graph& g; void create_graph(){ \\ create graph } and in modify_graph.cpp extern Graph& g; void modify_graph(){ \\... } -James Thunes
On Wed, 28 Oct 2009, James wrote:
Jeremiah Willcock wrote:
On Wed, 28 Oct 2009, James wrote:
Jeremiah Willcock wrote:
You could also just declare the graph in whichever scope will end up storing it and then pass it by reference to the construction function:
void function(Graph& g) { // build g, or build a temporary and assign it to g }
Graph the_graph; function(the_graph);
Where are you going to be putting the final graph? Will it be in a variable in your main(), even if it is not constructed there?
-- Jeremiah Willcock Jeremiah,
I'm sorry if I was not clear in my first post. The majority of the code (along with the main routine) is written in Fortran. I am using the graph to update a number of arrays which are needed for this Fortran code. I will create the graph in a subroutine (or c++ function) and modify it in another c++ function. The graph is not called in the main routine, only in subroutines.
I need to know the way the graph is stored so I can pass it back to the main function (or perhaps define it as a public variable).
In reference to the code snippet above, am I correct in reading it as the creating routine will be defined as: void function(Graph& g) { // build g, or build a temporary and assign it to g } and then the function to modify it would be defined as: void modify_the_graph(Graph& g){ \\ some graph manipulations here }
Where is the variable holding the graph going to be? If it doesn't have a Fortran type the variable cannot be in Fortran code. Are you going to have a global variable (in C++) that contains the graph? Otherwise, you might need to use a raw (not shared) pointer to the graph, managing the memory yourself, and pass it between Fortran and C++ as either some kind of interoperability C pointer type (in newer Fortran versions), a large enough integer, or maybe a Cray pointer.
-- Jeremiah Willcock
That is the issue. I agree that to manipulate the graph in the Fortran portion of the code would be difficult (due the lack of a comparable data type). Luckily, I don't need to modify the graph in the Fortran portion of the code, I just need to allow for a c++ function to access the graph. If I can define the graph as a global variable in c++ and negate the need to pass the graph from the create_graph function to the modify_graph function via the Fortran main routine: Create_Graph.cpp -> Main.f -> Modify_Graph.cpp would be great.
Would I accomplish this as: in create_graph.cpp Graph& g;
void create_graph(){ \\ create graph }
and in modify_graph.cpp extern Graph& g;
void modify_graph(){ \\... }
The storage for the graph needs to be somewhere; making the variables of type Graph rather than Graph& would solve that problem. -- Jeremiah Willcock
On Thu, Oct 29, 2009 at 8:47 AM, Jeremiah Willcock
On Wed, 28 Oct 2009, James wrote:
That is the issue. I agree that to manipulate the graph in the Fortran
portion of the code would be difficult (due the lack of a comparable data type). Luckily, I don't need to modify the graph in the Fortran portion of the code, I just need to allow for a c++ function to access the graph. If I can define the graph as a global variable in c++ and negate the need to pass the graph from the create_graph function to the modify_graph function via the Fortran main routine: Create_Graph.cpp -> Main.f -> Modify_Graph.cpp would be great.
Would I accomplish this as: in create_graph.cpp Graph& g;
void create_graph(){ \\ create graph }
and in modify_graph.cpp extern Graph& g;
void modify_graph(){ \\... }
The storage for the graph needs to be somewhere; making the variables of type Graph rather than Graph& would solve that problem.
-- Jeremiah Willcock
That worked. With the two functions as: create_graph.cpp Graph g;
void create_graph(){ \\... } and modify_graph.cpp extern Graph g; void modify_graph(){ \\... } The code compiles and runs correctly for my test case. Thanks for the help!
participants (4)
-
James
-
James Thunes
-
Jeremiah Willcock
-
Sandeep Gupta