unresolved call 'init' in Domain Map Class when implementing DSI - chapel

(chpl version 1.16.0.e43acc7)
I'm starting to learn the DSI interface and have run into a confusing issue when constructing a Domain class from the dsiNewRectangularDom function in the Distribution class:
class MyDist : BaseDist {
proc MyDist( fold_dimensions ...?dims ){ }
proc dsiNewRectangularDom(param rank: int, type idxType, param stridable: bool, inds) {
var dom = new MyDom( rank=rank, idxType=idxType, stridable=stridable, dist=this);
return dom;
}
}
class MyDom : BaseRectangularDom { }
class MyArr : BaseArr { }
config const n = 4;
config const m = 8;
const base_domain = {1..#n,1..#m};
const mapped_domain = base_domain dmapped MyDist( 1 );
(This is very basic code, and I don't expect it to fully compile, but I'm stuck on this part.)
This produces the compile error:
file.chpl:5: In function 'dsiNewRectangularDom':
file.chpl:6: error: unresolved call 'MyDom.init(rank=2, idxType=type int(64), stridable=0, dist=MyDist)'
file.chpl:11: note: candidates are: MyDom.init(_arrs, _arrs_containing_dom: int(64), _arrsLock: atomicbool, _free_when_no_arrs: bool, pid: int(64), param rank: int(64), type idxType, param stridable: bool)
(see this TIO instance)
I'm a bit confused about where this init function comes from.
I'm following the behavior of Block, BlockDist, and BlockDom (in particular BlockDist.chpl:533
where Block.dsiNewRectangularDom calls BlockDom's constructor.
Since MyDom inherits from BaseRectangularDom, I (1) don't need to declare the rank, idxType, etc member variables, and (2) don't need to define the MyDom( rank, idxType, ... ) constructor.
I also don't see a BlockDom.init function that I could learn from.
What am I missing?

Your most immediate problem is that BaseRectangularDom (and therefore MyDom) does not have a field named 'dist'. Like BlockDom, you'll want to add a 'dist' field, probably like:
var dist : MyDist;
Once you fix that, you'll be on to the next error (dsiAssignDomain is not implemented).
The error message likely mentioned 'init' as a side effect of the ongoing conversion from constructors to initializers.

Related

Conditionally adding arguments to a constructor (C++)

Is there a way to conditionally add arguments to a constructor? I'd also like to know what this type of construction is called so I can search it myself.
I'm creating a boost::process::child using a constructor where I can pass any properties and things are mostly working great:
m_proc = new boost::process::child(
m_context,
boost::process::exe = m_config.exe,
boost::process::args = m_config.args,
boost::process::env = m_config.Environment,
boost::process::start_dir = m_config.WorkingDirectory,
boost::process::std_out > m_stdout_pipe,
boost::process::std_err > m_stderr_pipe,
boost::process::on_exit = [this](int i, auto e){OnProcExit(i, e);},
boost::process::extend::on_setup = [this](auto&){OnProcSetup();},
boost::process::extend::on_success = [this](auto&){OnProcSuccess();},
boost::process::extend::on_error = [this](auto&, auto ec){OnProcError(ec);}
);
UNTIL I call ls with no arguments. Then it returns
/usr/bin/ls: cannot access '': No such file or directory
Process Exited (code:2)
If m_config.args is empty, I want to avoid passing it. I tried:
m_proc = new boost::process::child(
...
boost::process::exe = m_config.exe,
m_config.args.empty() ? (void) : (boost::process::args = m_config.args),
...
);
but that gives:
error: expected primary-expression before ‘void’
I tried:
m_proc = new boost::process::child(
...
boost::process::exe = m_config.exe,
boost::process::args = m_config.args.empty() ? {} : m_config.args,
...
But that gives:
initializer list cannot be used on the right hand side of operator ?
error: expected primary-expression before ‘{’ token
I understand that for this particular case, I could combine exe and args to make a cmd, but I'd also like to conditionally add other arguments like boost::process::shell or boost::process::stdin.
If I need to call different constructor code for every set of options, I would need to write N! calls to constructors where N is the number of options and that grows fast.
This is ugly.
C++ isn't python, there isn't any named parameters in C++, so this solution makes use of global variables (boost::process::args) which are fundamentally not thread safe and prone to usage errors as you experienced.
In the boost documentation they state you don't need to use the global vars, so you can directly use the your config members here:
m_proc = new boost::process::child(
m_context,
m_config.exe,
m_config.args,
m_config.Environment,
[...]
By the way, the error you're reporting isn't due to a bad empty list passing (you can have m_config.args = {}) but probably to a wrong command argument list creation (if the list is empty, the boost::process::child code should create an non-empty string for the process's argument list, containing the executable name as the first argument).
The error you are reporting:
/usr/bin/ls: cannot access '': No such file or directory
is likely due to the wrong argument list being generated by boost and it's not due to your (empty) args array.
So I would put a debug breakpoint on the Popen syscall here and walk backward until I figure out what went wrong in building the argument string.
Using a global variable like a parameter here is syntax candy, but it means operator overloading to an unspecified object (as stated in the documentation), so you actually don't know what's going on here (a = b when a is unknown can be anything in C++, like a crazy making b set to the value of a as in T& operator =(T & b) { b = *this; return *this; }). You'll need to debug what's going on here to figure out what is happening. As a short advice, try to avoid using undefined object if it's not required and unclear, and stick to usual C++ practices.

What's the proper way to have a Task that calls an arbitrary function with a known, specific return type?

I have a value which is expensive to calculate and can be asked for ahead of time--something like a lazily initiated value whose initialization is actually done at the moment of definition, but in a different thread. My immediate thought was to use parallelism.-Task seems purpose-built for this exact use-case. So, let's put it in a class:
class Foo
{
import std.parallelism : Task,task;
static int calculate(int a, int b)
{
return a+b;
}
private Task!(calculate,int,int)* ourTask;
private int _val;
int val()
{
return ourTask.workForce();
}
this(int a, int b)
{
ourTask = task!calculate(a,b);
}
}
That seems all well and good... except when I want the task to be based on a non-static method, in which case I want to make the task a delegate, in which case I start having to do stuff like this:
private typeof(task(&classFunc)) working;
And then, as it turns out, typeof(task(&classFunc)), when it's asked for outside of a function body, is actually Task!(run,ReturnType!classFunc function(Parameters!classFunc))*, which you may notice is not the type actually returned by runtime function calls of that. That would be Task!(run,ReturnType!classFunc delegate(Parameters!classFunc))*, which requires me to cast to typeof(working) when I actually call task(&classFunc). This is all extremely hackish feeling.
This was my attempt at a general template solution:
/**
Provides a transparent wrapper that allows for lazy
setting of variables. When lazySet!!func(args) is called
on the value, the function will be called in a new thread;
as soon as the value's access is attempted, it'll return the
result of the task, blocking if it's not done calculating.
Accessing the value is as simple as using it like the
type it's templated for--see the unit test.
*/
shared struct LazySet(T)
{
/// You can set the value directly, as normal--this throws away the current task.
void opAssign(T n)
{
import core.atomic : atomicStore;
working = false;
atomicStore(_val,n);
}
import std.traits : ReturnType;
/**
Called the same way as std.parallelism.task;
after this is called, the next attempt to access
the value will result in the value being set from
the result of the given function before it's returned.
If the task isn't done, it'll wait on the task to be done
once accessed, using workForce.
*/
void lazySet(alias func,Args...)(Args args)
if(is(ReturnType!func == T))
{
import std.parallelism : task,taskPool;
auto t = task!func(args);
taskPool.put(t);
curTask = (() => t.workForce);
working = true;
}
/// ditto
void lazySet(F,Args...)(F fpOrDelegate, ref Args args)
if(is(ReturnType!F == T))
{
import std.parallelism : task,taskPool;
auto t = task(fpOrDelegate,args);
taskPool.put(t);
curTask = (() => t.workForce);
working = true;
}
private:
T _val;
T delegate() curTask;
bool working = false;
T val()
{
import core.atomic : atomicStore,atomicLoad;
if(working)
{
atomicStore(_val,curTask());
working = false;
}
return atomicLoad(_val);
}
// alias this is inherently public
alias val this;
}
This lets me call lazySet using any function, function pointer or delegate that returns T, and then it'll calculate the value in parallel and return it, fully calculated, next time anything tries to access the underlying value, exactly as I wanted. Unit tests I wrote to describe its functionality pass, etc., it works perfectly.
But one thing's bothering me:
curTask = (() => t.workForce);
Moving the Task around by way of creating a lambda on-the-spot that happens to have the Task in its context still seems like I'm trying to "pull one over" on the language, even if it's less "hackish-feeling" than all the casting from earlier.
Am I missing some obvious language feature that would allow me to do this more "elegantly"?
Templates that take an alias function parameter (such as the Task family) are finicky regarding their actual type, as they can receive any type of function as parameter (including in-place delegates that get inferred themselves). As the actual function that gets called is part of the type itself, you would have to pass it to your custom struct to be able to save the Task directly.
As for the legitimacy of your solution, there is nothing wrong with storing lambdas to interact with complicated (or "hidden") types later.
An alternative is to store a pointer to &t.workForce directly.
Also, in your T val() two threads could enter if(working) at the same time, but I guess due to the atomic store it wouldn't really break anything - anyway, that could be fixed by core.atomic.cas.

How to bind a std::map to Lua with LuaBind

I'm trying to expose my std::map<std::string, std::string> as a class property to Lua. I've set this method for my getter and setter:
luabind::object FakeScript::GetSetProperties()
{
luabind::object table = luabind::newtable(L);
luabind::object metatable = luabind::newtable(L);
metatable["__index"] = &this->GetMeta;
metatable["__newindex"] = &this->SetMeta;
luabind::setmetatable<luabind::object, luabind::object>(table, metatable);
return table;
}
This way it makes me able to do something like this in Lua:
player.scripts["movement"].properties["stat"] = "idle"
print(player.scripts["movement"].properties["stat"])
However, the code I've provided in C++ doesn't getting compiled. It tells me there is an ambiguous call to overloaded function at this line metatable["__index"] = &this->GetMeta; and the line after it. I'm not sure that I'm doing this correctly.
Error message:
error C2668: 'luabind::detail::check_const_pointer' :
ambiguous call to overloaded function
c:\libraries\luabind-0.9.1\references\luabind\include\luabind\detail\instance_holder.hpp 75
These are SetMeta and GetMeta in FakeScript:
static void GetMeta();
static void SetMeta();
Previously I was doing this for getter method:
luabind::object FakeScript::getProp()
{
luabind::object obj = luabind::newtable(L);
for(auto i = this->properties.begin(); i != this->properties.end(); i++)
{
obj[i->first] = i->second;
}
return obj;
}
This works fine, but it's not letting me to use setter method. For example:
player.scripts["movement"].properties["stat"] = "idle"
print(player.scripts["movement"].properties["stat"])
In this code it just going to trigger getter method in both lines. Although if it was letting me to use setter, I wouldn't be able to get key from properties which it is ["stat"] right here.
Is there any expert on LuaBind here? I've seen most of people say they've never worked with it before.
You need to use the (undocumented) make_function() to make objects from functions.
metatable["__index"] = luabind::make_function(L, &this->GetMeta);
metatable["__newindex"] = luabind::make_function(L, &this->GetMeta);
Unfortunately, this (the simplest) overload of make_function is broken, but you just need to insert f as the second parameter in make_function.hpp.

C++ - Smart Pointers - Passing derived class shared pointer to base through template

I have the following and having difficulty resolving the error please help.
i have the following class as template definition somewhere.
template<class ConcreteHandlerType>
class SomeAcceptor: public ACE_Acceptor<ConcreteHandlerType, ACE_SOCK_Acceptor>
In some other file, i initialize this class in the constructor
class initialize {
typedef SomeAcceptor<BaseClassSomeHandler> baseAcceptor_t;
typedef SomeAcceptor<DerivedClassSomeHandler> derivedAcceptor_t;
boost::shared_ptr<baseAcceptor_t;> mAcceptor;
boost::shared_ptr<derivedAcceptor_t;> mDerivedAcceptor;
bool HandleAcceptNotification(BaseClassSomeHandler& someHandler);
initialize() : mAcceptor(0), mDerivedAcceptor(new DerivedAcceptor_t) {
mAcceptor->SetAcceptNotificationDelegate(fastdelegate::MakeDelegate(this, &initialize::HandleAcceptNotification));
}
}
Error i get is
error: no matching function for call to `boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >::shared_ptr(int)'common/lib/boost_1_39_0/boost/smart_ptr/shared_ptr.hpp:160: note: candidates are: boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >::shared_ptr(const boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >&)
common/lib/boost_1_39_0/boost/smart_ptr/shared_ptr.hpp:173: notboost::shared_ptr<T>::shared_ptr() [with T = SomeAcceptor<BaseClassSomeHandler>]
I also tried overloading the function with
bool HandleAcceptNotification(DerivedClassSomeHandler& someHandler);
but because mAcceptor is of type SomeAcceptor BaseClassSomeHandler, i get this error, but to fix this.
I guess i need to cast it somehow, but how to do it?
i tried doing like below inside the constructor and it didn't work
initialize() : mAcceptor(0), mDerivedAcceptor(new DerivedAcceptor_t) {
mAcceptor = mDerivedAcceptor; // Error here
mAcceptor->SetAcceptNotificationDelegate(fastdelegate::MakeDelegate(this, &initialize::HandleAcceptNotification));
}
From your code, it looks like you want mAcceptor to be assigned NULL (0), if that is the case you don't need to initialize it at all, as the default constructor will take care of that. But, since you call a function on that (NULL) pointer immediately, its not immediately clear exactly what you want to do.
If you want mAcceptor and mDerivedAcceptor to point to the same (shared) object and assuming DerivedClassSomeHandler is derived from BaseClassSomeHandler, this is a situation where you should use boost::shared_static_cast, as described here.
There's also some good information in this apparently related question.
The error is due to the mAcceptor(0) in
initialize() : mAcceptor(0), mDerivedAcceptor(new DerivedAcceptor_t) {
mAcceptor->SetAcceptNotificationDelegate(fastdelegate::MakeDelegate(this, &initialize::HandleAcceptNotification));
}
The smart_ptr default constructor assigns the wrapped ptr to NULL, so leave out mAcceptor(0) from the initialization list.
boost::shared_ptr<SomeAcceptor<BaseClassSomeHandler> >::shared_ptr(int)
It's yelling at you that there's no constructor that accepts an int.
Just use: mAcceptor()

error: no matching function for call to

I am getting the "no matching function for call to error" in my code. The code is creating instances of one class in the constructor of another class.
The code is as follows:
inline DiscriminatorContainer::DiscriminatorContainer(ushort id, FebPtr feb):
m_id(id), m_feb(feb), m_discriminators(new Discriminators()) {
//make discriminators
for (ushort i = 0; i <kNDiscriminators; ++i){
DiscriminatorPtr dsc = DiscriminatorPtr(new Discriminator(i, this));
m_discriminators->push_back(dsc);
}
}
inline Discriminator::Discriminator(ushort id, DiscriminatorContainerPtr dc, double threshold) :
m_id(id), m_threshold(threshold),
m_nhits(0), m_dc(dc)
{
init();
}
These constructors are actually in two different header files, but I included both for completeness.
I get the error
../src/DiscriminatorContainer.h:50: error: no matching function for call to `Minerva::Discriminator::Discriminator(ushort&, Minerva::DiscriminatorContainer* const)'
../src/Discriminator.h:24: note: candidates are: Minerva::Discriminator::Discriminator(const Minerva::Discriminator&)
../src/Discriminator.h:61: note: Minerva::Discriminator::Discriminator(ushort, Minerva::DiscriminatorContainerPtr, double)
about the line that goes DiscriminatorPtr dsc = ....
I know that such an error usually means I've got a wrong data type somewhere, but I can't figure out where it could be?
Typical case of "read the error message more carefully".
It's telling you that you are trying to call the function with the signature Minerva::Discriminator::Discriminator(ushort&, Minerva::DiscriminatorContainer* const)
but there only exists two other constructors
Minerva::Discriminator::Discriminator(const Minerva::Discriminator&)
Minerva::Discriminator::Discriminator(ushort, Minerva::DiscriminatorContainerPtr, double)
So just check the call to the constructor. You probably forget one argument.
new Discriminator(i, this) does not match any constructors.
Edit
Well, inlining doesn't affect default arguments so you must have made a mistake somewhere else!