Hello,

I can see how this is a problem. In this case, I assume that it is impossible 
to pass the C++ function to the topology configuration file. I assume that I 
will have to use parameters instead. 

addChild is used to add a MachineID to a C++ vector. I suppose that I can 
create a list of MachineIDs in Python and add the list to the ChildStates as a 
parameter. I am wondering how the python list can be converted to a C++ vector 
object, as I do not know how.

Best regards,

Alex


________________________________
 From: Steve Reinhardt <ste...@gmail.com>
To: Alex Tomala <alegomas...@yahoo.ca> 
Cc: gem5 mailing list <gem5-users@gem5.org> 
Sent: Thursday, January 2, 2014 7:00:43 PM
Subject: Re: [gem5-users] Calling a C++ function in Python
 


OK, I think I see one potential problem... note that in SimObject.py where 
you're getting the error we have this code:

        # If the attribute exists on the C++ object, transparently
        # forward the reference there.  This is typically used for
        # SWIG-wrapped methods such as init(), regStats(),
        # resetStats(), startup(), drain(), and
        # resume().
        if self._ccObject and hasattr(self._ccObject, attr):
            return getattr(self._ccObject, attr)

        raise AttributeError, "object '%s' has no attribute '%s'" \
              % (self.__class__.__name__, attr)

So in spite of the text of the error message, you could be failing because 
_ccObject (the pointer to the swig-wrapped C++ object) is not set up (i.e., 
because the corresponding C++ object has not been created), not because the 
attribute doesn't exist.  (Or strictly speaking, because the attribute won't 
exist on the swig-wrapped C++ object once it has been created.)  Given that 
your code snippet shows you calling the constructor and then trying to call 
addChild() right away without a call to root.instantiate(), I'm guessing that's 
your problem here.

Steve




On Thu, Jan 2, 2014 at 3:48 PM, Alex Tomala <alegomas...@yahoo.ca> wrote:

The method seems to show up in both files, which I have attached to this email. 
Looking over the SWIG documentation briefly, I found no problems with the files.
>
>
>- Alex
>
>
>
>________________________________
> 
>From: Steve Reinhardt <ste...@gmail.com>
>To: Alex Tomala <alegomas...@yahoo.ca> 
>Cc: gem5 mailing list <gem5-users@gem5.org> 
>Sent: Thursday, January 2, 2014 6:35:43 PM
>
>Subject: Re: [gem5-users] Calling a C++ function in Python
>
>
>
>And yet the addChild method is showing up in 
>build/*/python/m5/internal/param_ChildStates.i?  Is it also in the 
>param_ChildStates.py file in that same directory?
>
>
>
>On Thu, Jan 2, 2014 at 3:29 PM, Alex Tomala <alegomas...@yahoo.ca> wrote:
>
>Hello,
>>
>>
>>In the topology configuration file I accidentally did 
>>childStates.getChild(child), instead of childStates.addChild(child). When I 
>>fixed it, I still got the exact same error, which is:
>>
>>
>>  childStates.addChild(child)
>>
>>  File "/home/alex/Desktop/gem5/gem5-stable/src/python/m5/SimObject.py", line 
>>725, in __getattr__
>>    % (self.__class__.__name__, attr)
>>AttributeError: object 'ChildStates' has no attribute 'addChild'
>>
>>
>>Best regards,
>>
>>
>>Alex Tomala
>>
>>________________________________
>> 
>>From: Steve Reinhardt <ste...@gmail.com>
>>To: Alex Tomala <alegomas...@yahoo.ca> 
>>Cc: gem5 users mailing list <gem5-users@gem5.org> 
>>Sent: Thursday, January 2, 2014 6:16:24 PM
>>
>>Subject: Re: [gem5-users] Calling a C++ function in Python
>>
>>
>>
>>I'm a little confused about where the 'getChild' call is coming from since 
>>'addChild' is the method you added... are you leaving some parts out of the 
>>code you're posting?
>>
>>
>>
>>On Thu, Jan 2, 2014 at 3:09 PM, Alex Tomala <alegomas...@yahoo.ca> wrote:
>>
>>Hello Steve,
>>>
>>>
>>>I tried the following in a topology configuration file:
>>>
>>>
>>>childStates = ChildStates()
>>>childStates.addChild(child) #child is the MachineID
>>>
>>>
>>>When I run gem5 I get the following error:
>>>
>>>
>>>  childStates.getChild(child)
>>>
>>>  File "/home/alex/Desktop/gem5/gem5-stable/src/python/m5/SimObject.py", 
>>>line 725, in __getattr__
>>>    % (self.__class__.__name__, attr)
>>>AttributeError: object 'ChildStates' has no attribute 'getChild'
>>>
>>>
>>>This is the only method I tried, but I can't think of any other method to 
>>>get it done.
>>>
>>>
>>>One day I may add some information to the wiki, but I am a tad busy now.
>>>
>>>
>>>Best regards,
>>>
>>>
>>>Alex Tomala
>>>
>>>________________________________
>>>
>>>From: Steve Reinhardt <ste...@gmail.com>
>>>To: Alex Tomala <alegomas...@yahoo.ca>; gem5 users mailing list 
>>><gem5-users@gem5.org> 
>>>Sent: Thursday, January 2, 2014 5:22:47 PM
>>>Subject: Re: [gem5-users] Calling a C++ function in Python
>>> 
>>>
>>>
>>>What have you tried and what problems are you having?  It should just be a 
>>>matter of calling 'obj.addChild(x)' on your ChildStates object 'obj'.
>>>
>>>
>>>It looks like you've figured most of this out already, but a lot of the 
>>>current mechanism was added in this changeset (see particularly the comments 
>>>in the added code in SimObject.py):
>>>http://repo.gem5.org/gem5/rev/45c9f664a365
>>>
>>>
>>>And here's an example of adding a method:
>>>http://repo.gem5.org/gem5/rev/09dcdb40b109
>>>
>>>
>>>If you're feeling ambitious, it would be great if you'd be willing to write 
>>>up some documentation on how to add callable functions on the wiki, say on 
>>>this page (or a page linked from it):
>>>http://gem5.org/SimObjects
>>>
>>>
>>>
>>>Thanks,
>>>
>>>
>>>Steve
>>>
>>>
>>>
>>>
>>>On Thu, Jan 2, 2014 at 1:38 PM, Alex Tomala <alegomas...@yahoo.ca> wrote:
>>>
>>>Hello,
>>>>
>>>>
>>>>I partially figure out the solution to the problem. I figured out that you 
>>>>can over ride the exports_method method from the sim object python file to 
>>>>add functions. Now my Python interfacing file looks like this:
>>>>
>>>>
>>>> from m5.params import *
>>>>from m5.SimObject import SimObject
>>>>from Controller import RubyController
>>>>
>>>>
>>>>class ChildStates(SimObject):
>>>>type = 'ChildStates'
>>>>cxx_class = 'ChildStates'
>>>>cxx_header = '"mem/protocol/ChildStates.hh"
>>>>childPorts = Param.Int("Number of Children Ports")
>>>>@classmethod
>>>>def export_methods(cls, code):
>>>>        code('''
>>>>void addChild(MachineID child);
>>>>''')
>>>>
>>>>
>>>>The *.i file associated with the custom SimObject now contains the proper 
>>>>method. My only problem that I have now is that I don't know how to call 
>>>>the method in Python. I am guessing that I am importing the wrong module or 
>>>>something similar.  
>>>>
>>>>
>>>>- Alex
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>________________________________
>>>> From: Alex Tomala <alegomas...@yahoo.ca>
>>>>To: gem5 mailing list <gem5-users@gem5.org> 
>>>>Sent: Wednesday, January 1, 2014 5:03:13 PM
>>>>Subject: Calling a C++ function in Python
>>>> 
>>>>
>>>>
>>>>Hello,
>>>>
>>>>
>>>>I created a custom SimObject in C++ that has a function that I want to 
>>>>execute in Python. I am wondering how I can interface the function to 
>>>>Python as instance.function_name does not seem to work. The function I am 
>>>>using is called addChild and it takes a MachineID as an input.
>>>>
>>>>
>>>>My current Python interfacing file looks like this:
>>>>
>>>>
>>>>from m5.params import *
>>>>from m5.SimObject import SimObject
>>>>from Controller import RubyController
>>>>
>>>>
>>>>class ChildStates(SimObject):
>>>>type = 'ChildStates'
>>>>cxx_class = 'ChildStates'
>>>>cxx_header = '"mem/protocol/ChildStates.hh"
>>>>childPorts = Param.Int("Number of Children Ports")
>>>>
>>>>
>>>>Best regards,
>>>>
>>>>
>>>>Alex
>>>>
>>>>
>>>>_______________________________________________
>>>>gem5-users mailing list
>>>>gem5-users@gem5.org
>>>>http://m5sim.org/cgi-bin/mailman/listinfo/gem5-users
>>>>
>>>
>>>
>>>
>>
>>
>>
>
>
>
_______________________________________________
gem5-users mailing list
gem5-users@gem5.org
http://m5sim.org/cgi-bin/mailman/listinfo/gem5-users

Reply via email to