View mode: basic / threaded / horizontal-split · Log in · Help
August 12, 2012
char ***argc problems.
I'm attempting to create a wrapper for MPI, however, MPI_Init
wants to read the arguments for main():

MPI_Init(int *argv, char ***argc);

How do I get this last level of pointer reference?

So far, I have:

void main (string[] args)
{
      auto argarr = new char*[args.length];
      foreach(i, a; args)
          argarr[i] = (a.dup ~ '\0').ptr;

      int argc = to!(int)(argarr.length);
      MPI_Init(&argc, argarr.ptr);
}

Any ideas?

-Andrew
August 12, 2012
Re: char ***argc problems.
On 08/12/2012 10:33 PM, Andrew wrote:
> I'm attempting to create a wrapper for MPI, however, MPI_Init
> wants to read the arguments for main():
>
> MPI_Init(int *argv, char ***argc);
>
> How do I get this last level of pointer reference?
>
> So far, I have:
>
> void main (string[] args)
> {
>     auto argarr = new char*[args.length];
>     foreach(i, a; args)
>     argarr[i] = (a.dup ~ '\0').ptr;
>
>     int argc = to!(int)(argarr.length);
>     MPI_Init(&argc, argarr.ptr);
> }
>
> Any ideas?
>
> -Andrew

Maybe like this?

import std.algorithm, std.array, std.conv;
void main(string[] args){
    auto argarr = args.map!(a=>(a ~ '\0').ptr).array;

    int argc = to!(int)(argarr.length);
    auto argv = argarr.ptr;

    MPI_Init(&argc, &argv);

    args = argv[0..argc].map!(to!string).array;

    // ...
}
August 12, 2012
Re: char ***argc problems.
On Sunday, 12 August 2012 at 20:57:34 UTC, Timon Gehr wrote:
> On 08/12/2012 10:33 PM, Andrew wrote:
>> I'm attempting to create a wrapper for MPI, however, MPI_Init
>> wants to read the arguments for main():
>>
>> MPI_Init(int *argv, char ***argc);
>>
>> How do I get this last level of pointer reference?
>>
>> So far, I have:
>>
>> void main (string[] args)
>> {
>>    auto argarr = new char*[args.length];
>>    foreach(i, a; args)
>>    argarr[i] = (a.dup ~ '\0').ptr;
>>
>>    int argc = to!(int)(argarr.length);
>>    MPI_Init(&argc, argarr.ptr);
>> }
>>
>> Any ideas?
>>
>> -Andrew
>
> Maybe like this?
>
> import std.algorithm, std.array, std.conv;
> void main(string[] args){
>     auto argarr = args.map!(a=>(a ~ '\0').ptr).array;
>
>     int argc = to!(int)(argarr.length);
>     auto argv = argarr.ptr;
>
>     MPI_Init(&argc, &argv);
>
>     args = argv[0..argc].map!(to!string).array;
>
>     // ...
> }

I don't know why I didn't think of this:

auto argv = argarr.ptr;

Which fixed it.

Unfortunately, using args.map! doesn't work, because it returns
an immutable, which MPI_Init can't take.

Thanks for the help

-Andrew
August 12, 2012
Re: char ***argc problems.
On 08/12/2012 11:38 PM, Andrew wrote:
> On Sunday, 12 August 2012 at 20:57:34 UTC, Timon Gehr wrote:
>> On 08/12/2012 10:33 PM, Andrew wrote:
>>> I'm attempting to create a wrapper for MPI, however, MPI_Init
>>> wants to read the arguments for main():
>>>
>>> MPI_Init(int *argv, char ***argc);
>>>
>>> How do I get this last level of pointer reference?
>>>
>>> So far, I have:
>>>
>>> void main (string[] args)
>>> {
>>>    auto argarr = new char*[args.length];
>>>    foreach(i, a; args)
>>>    argarr[i] = (a.dup ~ '\0').ptr;
>>>
>>>    int argc = to!(int)(argarr.length);
>>>    MPI_Init(&argc, argarr.ptr);
>>> }
>>>
>>> Any ideas?
>>>
>>> -Andrew
>>
>> Maybe like this?
>>
>> import std.algorithm, std.array, std.conv;
>> void main(string[] args){
>>     auto argarr = args.map!(a=>(a ~ '\0').ptr).array;
>>
>>     int argc = to!(int)(argarr.length);
>>     auto argv = argarr.ptr;
>>
>>     MPI_Init(&argc, &argv);
>>
>>     args = argv[0..argc].map!(to!string).array;
>>
>>     // ...
>> }
>
> I don't know why I didn't think of this:
>
> auto argv = argarr.ptr;
>
> Which fixed it.
>
> Unfortunately, using args.map! doesn't work, because it returns
> an immutable, which MPI_Init can't take.
>

oops.
auto argarr = args.map!(a=>(a.dup ~ '\0').ptr).array;

> Thanks for the help
>
> -Andrew
>
Top | Discussion index | About this forum | D home