nm

11353 Reputation

20 Badges

13 years, 1 days

MaplePrimes Activity


These are questions asked by nm

I am calling a proc inside a module, where this module had local module variable initialized to false;

This works fine when calling the proc normally.

When calling it from Grid:-Run() it says it is not seeing the module local variable at all.

Why does this happen and is there a workaround this? Here a simple worksheet showing this.

I hope there is a way to make the proc inside the module see the local module variables when using it from Grid node. Otherwise, this whole thing will not work for me, as I have few variables initialized at modules level in number of places.

restart;

A := module()
  local DEBUG_MSG::truefalse:=false;
  export work_proc:=proc(n::integer)
     print("My flag is ",DEBUG_MSG);
  end proc;
end module;

 

_m1690230418240

#THis works as expected
A:-work_proc(0)

"My flag is ", false

#this does not work. THe call works OK, but inside A:-workproc() it does not see module local variables.

Grid:-Set(A:-work_proc):
Grid:-Run(0,A:-work_proc,[ 0 ]);
Grid:-Wait();

"My flag is ", DEBUG_MSG

 

 

Download grid_with_module_local_variable_feb_7_2025.mw


Update 2/7/2025

I tried to change the access to the module local variables from the module local procs, by adding explicit A:- to each variable name where A here is the module name.

This works for normal calls, but not when using Grid to call the proc.

This workaround fail, it gives error that module does not export `%1`

May be I have to redo all my code not to use local module variables. But in some places I have to do this, in order to detect if something has happened before or not. I use module local variables to store state the presist after call is completed.

It looks like Grid is not meant to be used for calling proc() that uses/lives inside modules. But this makes Grid not very useful then for large application.

restart;

A := module()
  local DEBUG_MSG::truefalse:=false;
  export work_proc:=proc(n::integer)
     print("My flag is ",A:-DEBUG_MSG);
  end proc;
end module;

 

_m1690230418240

#THis works as expected
A:-work_proc(0)

"My flag is ", false

#this does not work. THe call works OK, but inside A:-workproc() it does not see module local variables.

Grid:-Set(A:-work_proc):
Grid:-Run(0,A:-work_proc,[ 0 ]);
Grid:-Wait();

Error, (in work_proc) module does not export `%1`

 

 

Download grid_with_module_local_variable_feb_7_2025_V2.mw

I also tried to make the module variable as export instead of local, and that also did not work. I am starting to run out of ideas what else to try...

restart;

A := module()
  export DEBUG_MSG::truefalse:=false;
  export work_proc:=proc(n::integer)
     print("My flag is ",A:-DEBUG_MSG);
  end proc;
end module;

 

_m1690230418208

#THis works as expected
A:-work_proc(0)

"My flag is ", false

#this does not work. THe call works OK, but inside A:-workproc() it does not see module local variables.

Grid:-Set(A:-work_proc):
Grid:-Run(0,A:-work_proc,[ 0 ]);
Grid:-Wait();

Error, (in work_proc) `%1` does not evaluate to a module

 

 

Download grid_with_module_local_variable_feb_7_2025_V3.mw

Update

I think now that using module level local variables will not work with parallel processing anyway. It is like using global variable in parallel processing. Does not work without synchorization of access to this shared variable. 

So this means I have to change my code and not use any module local variables, and pass any information between functions via arguments only.

Using module local variable is more convenient, but I now realize this design is not good for parallel processing.  This means some code changes I have to do. 

On the positive side, I find using Grid can really speed things up. On some tests, up to 10 times faster. The larger the number of problems to process, the more speed up is gained.

So I think it is worth to rewrite my code and remove any use of local level module variables.

Update Feb 11, 2024

Found the solution!  With grid, one must pass in all variables that to be used on a node. So the above now becomes this

restart;

A := module()
  export DEBUG_MSG::truefalse:=false;
  export work_proc:=proc(n::integer)
     print("My flag is ",DEBUG_MSG);
  end proc;
end module;
 

_m1739526597408

#THis works as expected
A:-work_proc(0)

"My flag is ", false

#this does not work. Because we did not pass all variables to be used at node
Grid:-Set(0,A:-work_proc);
Grid:-Run(0,A:-work_proc,[ 0 ]);
Grid:-Wait();

"My flag is ", DEBUG_MSG

#this now worksheetdir
Grid:-Set(0,A:-work_proc,'A:-DEBUG_MSG');
Grid:-Run(0,A:-work_proc,[ 0 ]);
Grid:-Wait();

"My flag is ", false

 

 

Download grid_with_module_V4_feb_11_2025.mw

Actually the above could be done easier this way. By passing in the name of the whole module A, now nodes are able to see all module A local vaiables. Like this

restart;

A := module()
  export DEBUG_MSG::truefalse:=false;
  export work_proc:=proc(n::integer)
     print("My flag is ",A:-DEBUG_MSG);
  end proc;
end module;
 

_m1739526597408

#THis works as expected
A:-work_proc(0)

"My flag is ", false

#this does not work. Because we did not pass the name of the A
Grid:-Set(0,A:-work_proc);
Grid:-Run(0,A:-work_proc,[ 0 ]);
Grid:-Wait();

Error, (in work_proc) `%1` does not evaluate to a module

#this now works, becuase we pass the module A
Grid:-Set(0,'A:-work_proc','A');
Grid:-Run(0,A:-work_proc,[ 0 ]);
Grid:-Wait();

"My flag is ", false

 

 

Download grid_with_module_V6_feb_11_2025.mw

And to be able to access global variables in another module from inside the worker module, we must pass the other module name. Here is an example

restart;

B := module()
  export some_value::integer:=10;
end module;

A := module()
  export work_proc:=proc(n::integer)
     print("the value is ",B:-some_value);
  end proc;
end module;
 

_m1739526597280

_m1739594742272

#THis works as expected
A:-work_proc(0)

"the value is ", 10

#this does not work. Because we did not pass the name of the other module B in this case
Grid:-Set(0,A:-work_proc);
Grid:-Run(0,A:-work_proc,[ 0 ]);
Grid:-Wait();

Error, (in work_proc) `%1` does not evaluate to a module

#this now works, becuase we pass the module name B to the node, so it sees it
Grid:-Set(0,'A:-work_proc','B');
Grid:-Run(0,A:-work_proc,[ 0 ]);
Grid:-Wait();

"the value is ", 10

 

 

Download grid_with_module_V5_feb_11_2025.mw

The main point, if one is using modules with Grid, names of all modules to be accessed from a node need to be passed in in the Set call. Otherwise the node will not see them.

Update For the above method to work, all module variables to be accessed at node, have to export and not local.

Update 2/11/2025  4 AM

I think I'll give up on Grid. It is completely useless. I found out that I have to make all my modules proc's export now, even though they are only used in that one module, just in order to call them from same module when running in Grid node.

Otherwise, I have to list each proc name in each module and sub module in the Set command. This is ridiculous design. I have 5,000 proc's. I am not going to list them all each time.

Here is an example where a boo() is local proc to a module. But it is not called, because the proc is local.

restart;

A := module()

  export foo:=proc()
       print("entering A:-foo(), before calling local proc boo()");
       boo(); #this call will not work in Grid setting
  end proc;

  local boo:=proc()
     print("inside local A:-foo()");
  end proc;
end module:

A:-foo();

"entering A:-foo(), before calling local proc boo()"

"inside local A:-foo()"

Grid:-Set(0,A:-foo,'A');
Grid:-Run(0,A:-foo,[0]);
Grid:-Wait();

"entering A:-foo(), before calling local proc boo()"

 


 

Download calling_proc_in_module_V1.mw

To make it work, I have to make all my local proc export, and not only that, I have to call it using A:-boo() as well. So the above becomes


 

restart;

A := module()

  export foo:=proc()
       print("entering A:-foo(), before calling local proc boo()");
       A:-boo(); #this call will not work in Grid setting
  end proc;

  export boo:=proc()
     print("inside local A:-foo()");
  end proc;
end module:

A:-foo();

"entering A:-foo(), before calling local proc boo()"

"inside local A:-foo()"

Grid:-Set(0,A:-foo,'A');
Grid:-Run(0,A:-foo,[0]);
Grid:-Wait();

"entering A:-foo(), before calling local proc boo()"

"inside local A:-foo()"

 

 

Download calling_proc_in_module_V2.mw

I am not going to edit 100,000 lines of Maple code and change all the procs to export and make sure each call have the fully qualified module name attched to it even though it is local to the module.

I will not use Grid. It simply does not work with modules. Maplesoft should fix Grid so code works as with minimal changes.

To select all pdf files in folder the command is

L1:=FileTools:-ListDirectory(currentdir(),'all','select'="*.pdf");

And to select all dvi files the command is 

 L2:=FileTools:-ListDirectory(currentdir(),'all','select'="*.dvi");

Is there a syntax to select both in one command? THis does not work as input to select must be string. It can not be list nor set.

  FileTools:-ListDirectory(currentdir(),'all','select'=["*.dvi","*.pdf"]);

So I end up doing

L1:=FileTools:-ListDirectory(currentdir(),'all','select'="*.pdf");
L2:=FileTools:-ListDirectory(currentdir(),'all','select'="*.dvi");
the_list := [ op(L1), op(L2) ];

If will be nice if Maple allows one to select based on different extensions in one command.  Imagine you have 5 different extensions to select. Now one has to issue 5 different commands, then collect all the 5 lists into one.

I've been evaluating Grid but I found some problems with SQLite DB when not using numnodes.

So I am now using numnodes option to setup explicitly number of nodes to use with Grid.

But the problem is now print() do not show on the screen from node code. So hard to debug. So I changed code to send debug messages to print files.

But now I find that the files do even get created when using numnodes. 

When removing numnodes option, the text file gets created. 

I am using FileTools:-Text:-WriteString() and FileTools:-Text:-Close() in the node code.
I also tried using fopen(). Both do not work. 

Same code works OK if I do not use numnodes. 

Any idea why the file do not get created when using numnodes? Worksheet to produce this is below.

I use C:\\tmp folder for testing. Feel free to change this. When I run the code and look in the folder, I do not see the text file there when using numnodes. 

It is possible the file is created but saved somewhere else on the system even though the full file name is given?

It seems to me now that when using numnodes option, there are some things that work and some things that do not work. I do not know if this is by design or a bug. Any one knows?

restart;

interface(version);

`Standard Worksheet Interface, Maple 2024.2, Windows 10, October 29 2024 Build ID 1872373`

Physics:-Version();

`The "Physics Updates" version in the MapleCloud is 1843 and is the same as the version installed in this computer, created 2025, January 25, 22:5 hours Pacific Time.`

Why task running on node do not create text file?

 

restart;

currentdir("C:\\TMP"): #change as needed

foo:=proc(n::integer)   
   local file_name::string;
   currentdir("C:\\TMP"): #change as needed
   file_name:=cat(currentdir(),"\\",n,"_file.txt");
   FileTools:-Text:-WriteString(file_name,cat("processing at node =",n));
   FileTools:-Text:-Close(file_name);
end proc:

Grid:-Set(foo):
Grid:-Setup("local",numnodes=1);
Grid:-Run(0,foo,[0]):
Grid:-Wait();

#No file "0_file.txt" is created in my C:\\TMP\ folder

 

 

 

Another version using fopen instead of FileTools. This also do not work

 

restart;

currentdir("C:\\TMP"): #change as needed

foo:=proc(n::integer)   
   local file_name::string, fileID;
   currentdir("C:\\TMP"): #change as needed
   file_name:=cat(currentdir(),"\\",n,"_file.txt");
   try
        fileID := fopen(file_name,WRITE);
    catch:
        error StringTools:-FormatMessage(lastexception[2..-1]);
    end try;   

   fprintf(fileID,"%s",cat("processing at node =",n));
   fclose(fileID);        
end proc:

Grid:-Set(foo):
Grid:-Setup("local",numnodes=1);
Grid:-Run(0,foo,[0]):
Grid:-Wait();


Download grid_question_jan_29_2025.mw

To see the file 0_file.txt get created in the folder, simply remove the numnodes option.

I first tried Threads and found that Maple dsolve does not work in threads (see https://www.mapleprimes.com/questions/239602-Error-in-Dsolve-Type-System-Does)

It was suggested there to use Grid instead of Threads. 

Now I got time to try Grid. My first test shows that Grid does not work with dsolve also.

Here is an example where dsolve solves this system of odes,. But when using Grid, Maple gives an internal error 

         Error, (in evalapply) cannot apply non-operator differential equation

Does this means one can't use Threads and also can't use Grid with dsolve? Or Am I doing something wrong?

interface(version);

`Standard Worksheet Interface, Maple 2024.2, Windows 10, October 29 2024 Build ID 1872373`

Physics:-Version();

`The "Physics Updates" version in the MapleCloud is 1841 and is the same as the version installed in this computer, created 2025, January 3, 8:59 hours Pacific Time.`

restart;

P:=[diff(x(t),t)=t*x(t)-y(t)+exp(t)*z(t),diff(y(t),t)=2*x(t)+t^2*y(t)-z(t),diff(z(t),t)=exp(-t)*x(t)+3*t*y(t)+t^3*z(t)]:

dsolve(P); #no error, Long answer

{x(t) = (exp(t)*y(t)*t^5-(diff(y(t), t))*exp(t)*t^3-2*(exp(t))^2*y(t)*t^2-(diff(y(t), t))*exp(t)*t^2+2*(diff(y(t), t))*(exp(t))^2+t*y(t)*exp(t)+(diff(diff(y(t), t), t))*exp(t)+2*exp(t)*y(t))/(-2*t^3*exp(t)+4*(exp(t))^2+2*exp(t)*t-1), y(t) = DESol({diff(diff(diff(_Y(t), t), t), t)+(-4*(exp(t))^3/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-2*(exp(t))^2/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-exp(t)/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-4*(exp(t))^3*t^2/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-2*(exp(t))^2*t^3/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-4*(exp(t))^3*t/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+4*(exp(t))^2*t^2/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+exp(t)*t^3/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+2*(exp(t))^2*t^6/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+2*(exp(t))^2*t^5/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-4*(exp(t))^3*t^3/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+exp(t)*t^2/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+exp(t)*t/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t)))*(diff(diff(_Y(t), t), t))+(-4*(exp(t))^2/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-exp(t)/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-exp(t)*t^5/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+4*(exp(t))^3*t^2/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-6*(exp(t))^2*t^3/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-exp(t)*t^4/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-2*(exp(t))^2*t^8/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-2*(exp(t))^2*t^7/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+4*(exp(t))^3*t^5/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+4*(exp(t))^3*t^4/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+2*(exp(t))^2*t^5/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+4*(exp(t))^3*t^3/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-2*(exp(t))^2*t^4/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+2*(exp(t))^2*t/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+4*exp(t)*t^2/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+exp(t)*t/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t)))*(diff(_Y(t), t))+(-4*(exp(t))^3/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-8*(exp(t))^2/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-3*exp(t)/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+1/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-exp(t)*t^5/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-24*(exp(t))^4*t/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-8*(exp(t))^3*t^2/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+4*(exp(t))^2*t^3/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-5*exp(t)*t^4/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-4*(exp(t))^3*t/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+16*(exp(t))^2*t^2/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+4*exp(t)*t^3/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+6*(exp(t))^2*t/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+2*(exp(t))^2*t^9/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-4*(exp(t))^3*t^6/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-6*(exp(t))^2*t^7/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+4*(exp(t))^2*t^6/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+20*(exp(t))^3*t^4/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+8*(exp(t))^2*t^5/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))+exp(t)*t^6/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-12*(exp(t))^3*t^3/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-4*(exp(t))^2*t^4/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t))-3*exp(t)*t/(-2*(exp(t))^2*t^3+4*(exp(t))^3+2*(exp(t))^2*t-exp(t)))*_Y(t)}, {_Y(t)}), z(t) = (2*exp(t)*y(t)*t^3-2*(diff(y(t), t))*exp(t)*t^2-2*(diff(y(t), t))*exp(t)*t+2*t*y(t)*exp(t)-t^2*y(t)+2*(diff(diff(y(t), t), t))*exp(t)+4*exp(t)*y(t)+diff(y(t), t))/(-2*t^3*exp(t)+4*(exp(t))^2+2*exp(t)*t-1)}

restart;

 

P:=[diff(x(t),t)=t*x(t)-y(t)+exp(t)*z(t),diff(y(t),t)=2*x(t)+t^2*y(t)-z(t),diff(z(t),t)=exp(-t)*x(t)+3*t*y(t)+t^3*z(t)]:

Grid:-Run(0,dsolve(P)); #gives internal error
Grid:-Wait();

Error, (in evalapply) cannot apply non-operator differential equation

 


This error happens on this specific ode. I tried 2-3 others and did not see an error. So it seems to depend to what the ode is.

Download dsolve_also_fail_in_grid.mw

in help for Grid:-Wait, it has an example where it says

And in help for Grid:-Setup it says

"The numnodes option allows you to specify the number of nodes to be used in subsequent computations.  This option is only available in "local" mode."

Is numnodes supposed to be the same as number of cores on my PC?  If so, then why numnodes=4 says this will insure it run run on 2 core machine?

Is this typo and it should be 4 core machine?

First 9 10 11 12 13 14 15 Last Page 11 of 199