Message Boards Message Boards

1
|
2591 Views
|
5 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Assigning a name to variables depending on the thread number.

Posted 3 years ago

Hello,

I would like to name the variables dynamically, e.g. so that their names depend on the number of the thread being executed. Something like below:

n = 3;(*numer wątku*)
ParallelDo[
  Subscript[t, i] = Table[Random[], {10}], {i, n}];
t = Table[0, {10}];
Do[t += Subscript[t, i], {i, n}];
t

Unfortunately, table t has all zeros.

I tried to sum in a loop, I tried to use the $KernelID variable. Unfortunately, it didn't help.

The program "sees" the results because it can write them out:

n = 3;(*numer wątku*)
ParallelDo[
Subscript[t, i] = Table[Random[], {10}]; 
Print[Subscript[t, i]], {i, n}]

I am asking for a hint (it is certainly not very simple).

If "n" is greater than the number of threads, will the program (after the prompt) also work?

Best regards, DM

POSTED BY: D M
5 Replies

Hi DM,

In the case of ParallelDo you might have problems with defining the variables within different kernels (Something like DistributeDefinitions might help). However, if you try running your code with Do, instead of ParallelDo, you are gonna find that it still does not work. That is because Mathematica does not understand Subscript[t,i] as a symbol, despite the fact you can assign a value to it.

You might check that with, for example,:

Subscript[t,1]=1

then

Clear[Subscript[t,1]]

and you'll get the error "Clear::ssym: Subscript[t, 1] is not a symbol or a string." So, typing

t = Table[0, {10}];
Do[t += Subscript[t, i], {i, n}];

first assign a value for t (Table[0, {10}]) and then simply adds a Subscript to it (and that is why you're getting zeros). Thus, changing the name of your table is a solution (maybe from t to T).

Another way out is defining a vector t[[i]] instead of a variable with a subindex.

I hope it helps.

Posted 3 years ago

Thanks, did you try to write my code with your tip? Because I still don't know how to do it ...

POSTED BY: D M
Posted 3 years ago
Posted 3 years ago

Hi,

Because these arrays can be different in number, they can all be referenced at the same time (my Random [] is just an example).

POSTED BY: D M
Posted 3 years ago

Hi D M,

Can you explain the reason for wanting to do this

I would like to name the variables dynamically, e.g. so that their names depend on the number of the thread being executed.

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract