Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To array and to sparse functions are ready for linearoperator #368

Open
wants to merge 8 commits into
base: devel
Choose a base branch
from

Conversation

mateomarin97
Copy link

The default toarray and tosparse functions have been added to linearoperator.
Also, parallel tests for these functions have been written.

if BoS == "b":
comm = self.domain.spaces[0].cart.comm
elif BoS == "s":
comm = self.domain.cart.comm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is not a Blockvector or a Stencilvector then comm is not defined. I would add an assert in the beginning.

# We iterate over all the entries that belong to rank number currentrank
for i in itertools.product(*itterables):
if (rank == currentrank):
v[i] = 1.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a single line v[i] = (rank == currentrank)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to implement this using.
v[i] = float(rank == currentrank).
For the sequential tests, it worked. But for the parallel tests, it failed.
I am unsure why, so I left it as is.

else:
# I cannot conceive any situation where this error should be thrown, but I put it here just in case something unexpected happens.
raise Exception(
'Function toarray_struphy() only supports Stencil Vectors or Block Vectors.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant tosparse(). An assert at the beginning would be better

# V is either a BlockVector or a StencilVector depending on the domain of the linear operator.
if BoS == "b":
# we collect all starts and ends in two big lists
starts = [vi.starts for vi in v]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is possible to use the same code for both cases BoS == 'b' and BoS == 's' with some small changes, for instance:
starts = [vi.starts] if BoS == "s" else [vi.starts for vi in v]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although there are some similarities between the code for the BlockVectorSpaces and the StencilVectorSpaces, there are also a sizeable number of differences. Therefore, getting rid of the big if(BoS =="b")- elif(BoS =="s") would result in having to write a myriad of more tiny if-elif pairs. Which would negatively affect the readability of the code.

colarr.append(col)
row.append(l)
if (rank == currentrank):
v[i] = 0.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then v[i] -= (rank == currentrank)

psydac/linalg/basic.py Outdated Show resolved Hide resolved
#We need to determine if we are a blockvector or a stencilvector but we are not able to use
#the BlockVectorSpace and StencilVectorSpace classes in here. So we use the following char.
#This char is either a b for Blockvectors or an s for Stencilvectors
BoS= str(self.domain)[15]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If in the future this string changes then tosparse() would stop working. A potential alternative would be:
BoS = 'b' if hasattr(self.domain, 'spaces') else 's'

@yguclu
Copy link
Member

yguclu commented Oct 9, 2024

@mateomarin97 We see that you have made changes to the code after Elena's questions, but it is not clear what exactly they are for. Could you please answer explicitly?

@mateomarin97
Copy link
Author

@mateomarin97 We see that you have made changes to the code after Elena's questions, but it is not clear what exactly they are for. Could you please answer explicitly?

Hello. The changes were just aesthetic. I made the code more readable by implementing the suggestions Elena mentioned. The only thing stopping this function from being ready is that for the sparse matrix case, I could not write a Pyccel kernel to replace the for loop that appends the new matrices entries since Pyccel did not support Python lists.

As the code stands it works but it is too slow due to this problem.

@mateomarin97
Copy link
Author

@mateomarin97 We see that you have made changes to the code after Elena's questions, but it is not clear what exactly they are for. Could you please answer explicitly?

Hello. The changes were just aesthetic. I made the code more readable by implementing the suggestions Elena mentioned. The only thing stopping this function from being ready is that for the sparse matrix case, I could not write a Pyccel kernel to replace the for loop that appends the new matrices entries since Pyccel did not support Python lists.

As the code stands it works but it is too slow due to this problem.

Now that I think about it I am fairly certain I have a newer version of this branch that I did not push. I will push it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants