MPI_Send(100): Invalid rank has value 1 but must be nonnegative and less than 1 - mpi4py

I am learning MPI in python by myself. I just started from the basic documentation of MPI4py. I started with this code:
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
if rank == 0:
data = {'a': 7, 'b': 3.14}
comm.send(data, dest=1, tag=11)
elif rank == 1:
data = comm.recv(source=0, tag=11)
When I ran this program, I got following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "MPI/Comm.pyx", line 1175, in mpi4py.MPI.Comm.send (src/mpi4py.MPI.c:106424)
File "MPI/msgpickle.pxi", line 211, in mpi4py.MPI.PyMPI_send (src/mpi4py.MPI.c:42120)
mpi4py.MPI.Exception: Invalid rank, error stack:
MPI_Send(174): MPI_Send(buf=0x10e137554, count=25, MPI_BYTE, dest=1, tag=11, MPI_COMM_WORLD) failed
MPI_Send(100): Invalid rank has value 1 but must be nonnegative and less than 1
I didn't find any working solution for this problem. I am using Mac OS X El Capitan.
Thanks in Advance!

The program complains that 1 is not a valid rank for MPI_Send(): it means that your program is running on a single process.
Are you running it by using python main.py ? Try to use mpirun -np 2 python main.py, where 2 is the number of processes. The latter is the usual way to run mpi programs.

Related

How do I fix this EOF error on python 3 version

I am working on a very basic problem on Hackerrank.
Input format:
First line contains integer N.
Second line contains string S.
Output format:
First line should contain N x 2.
Second line should contain the same string S.
sample test case
5
helloworld
my code is as: (on PYTHON 3)
n=int(input())
s=input()
print(2*n)
print(s)
I am getting error:
Execution failed.
EOFError : EOF when reading a line
Stack Trace:
Traceback (most recent call last):
File "/tmp/143981299/user_code.py", line 1, in <module>
N = int(input())
EOFError: EOF when reading a line
I tried this method to take input many times and this is the first time I am having this error. Can anyone please explain why?
use a try/except block to handle the error
while True:
try:
n=int(input())
s=input()
print(2*n)
print(s)
except:
break

How can I remove Attribute error from my Csv program showing? [duplicate]

So I copied and pasted a demo program from the book I am using to learn Python:
#!/usr/bin/env python
import csv
total = 0
priciest = ('',0,0,0)
r = csv.reader(open('purchases.csv'))
for row in r:
cost = float(row[1]) * float(row[2])
total += cost
if cost == priciest[3]:
priciest = row + [cost]
print("You spent", total)
print("Your priciest purchase was", priciest[1], priciest[0], "at a total cost of", priciest[3])
And I get the Error:
Traceback (most recent call last):
File "purchases.py", line 2, in <module>
import csv
File "/Users/Solomon/Desktop/Python/csv.py", line 5, in <module>
r = csv.read(open('purchases.csv'))
AttributeError: 'module' object has no attribute 'read'
Why is this happening? How do I fix it?
Update:
Fixed All The Errors
Now I'm getting:
Traceback (most recent call last):
File "purchases.py", line 6, in <module>
for row in r:
_csv.Error: line contains NULL byte
What was happening in terms of the CSV.py:
I had a file with the same code named csv.py, saved in the same directory. I thought that the fact that it was named csv .py was screwing it up, so I started a new file called purchases.py, but forgot to delete csv
Don't name your file csv.py.
When you do, Python will look in your file for the csv code instead of the standard library csv module.
Edit: to include the important note in the comment: if there's a csv.pyc file left over in that directory, you'll have to delete that. that is Python bytecode which would be used in place of re-running your csv.py file.
There is a discrepancy between the code in the traceback of your error:
r = csv.read(open('purchases.csv'))
And the code you posted:
r = csv.reader(open('purchases.csv'))
So which are you using?
At any rate, fix that indentation error in line 2:
#!/usr/bin/env python
import csv
total = 0
And create your csv reader object with a context handler, so as not to leave the file handle open:
with open('purchases.csv') as f:
r = csv.reader(f)

Python 3.X pycomm.ab_comm.clx CommError: must be str, not bytes

Im trying to get "PYCOMM" to connect to my CLX 5000 processor.
Every time I run my code I get:CommError: must be str, not bytes.
I have looked all over the code and I cant find where the issue is. Everything that is supposed to be in a string format is.
I am using python3.6
Here is the code:
import sys
from pycomm.ab_comm.clx import Driver as ClxDriver
c = ClxDriver()
if c.open('172.16.2.161'):
print(c.read_tag('Start'))
# Prints (1,'BOOL') if true; (0,'BOOL') if false
c.close()
Here is the error:
C:\Users\shirley\Miniconda3\python.exe C:/Users/shirley/Downloads/pycomm-pycomm3/pycomm-pycomm3/examples/test_clx_comm.py
Traceback (most recent call last):
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 617, in build_header
h += pack_uint(length) # Length UINT
TypeError: must be str, not bytes
The header is 24 bytes fixed length, and includes the command and the length of the optional data portion.
:return: the headre
"""
try:
h = command # Command UINT
**h += pack_uint(length) # Length UINT**
h += pack_dint(self._session) # Session Handle UDINT
h += pack_dint(0) # Status UDINT
h += self.attribs['context'] # Sender Context 8 bytes
h += pack_dint(self.attribs['option']) # Option UDINT
return h
except Exception as e:
raise CommError(e)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 786, in open
if self.register_session() is None:
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 635, in register_session
self._message = self.build_header(ENCAPSULATION_COMMAND['register_session'], 4)
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 624, in build_header
raise CommError(e)
pycomm.cip.cip_base.CommError: must be str, not bytes
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/shirley/Downloads/pycomm-pycomm3/pycomm-pycomm3/examples/test_clx_comm.py", line 5, in
if c.open('172.16.2.161'):
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 793, in open
raise CommError(e)
pycomm.cip.cip_base.CommError: must be str, not bytes
Process finished with exit code 1
After banging my head against wall trying to figure this out on my own.
I just switched to using PYLOGIX
https://github.com/dmroeder/pylogix
It worked the first time I ran it
and its reasonably fast.
I had the same problem:
TypeError: must be str, not bytes.
The error is because you are using Python 3.x, and I did the same thing: I used Python 3.6 instead of Python 2.6 or 2.7.
Change Python 3.x to 2.6 or 2.7 (I used 2.7).
This was a bug that was fixed recently
I have it working with pycomm#1.0.8 and python#3.7.3

SyntaxError: unexpected EOF while parsing in Python 3

Code :
n,X=input(),0
for t in range(int(n)):
eval(input())
print(X)
Traceback (most recent call last):
File "prog.py", line 3, in <module>
eval(input())
File "<string>", line 1
X++
^
SyntaxError: unexpected EOF while parsing
Using raw_input instead of input() in the only solution I am able to find but in python 3.x input is raw_input(): How do I use raw_input in Python 3
any other method?
FYI; I am trying to solve: http://codeforces.com/problemset/problem/282/A
Remove the eval() call.
input() in Python 2 is the equivalent of eval(input()) in Python 3, and if you need to use raw_input() in Python 2 then in Python 3 you need to remove the eval() call.
You'll have to parse the input yourself; ++ is not a valid Python operator, you cannot use eval() to solve that Codeforces problem.
The simplest way to solve the posted problem is to read the input line by line:
import sys, itertools
count = int(next(sys.stdin))
x = 0
for line in itertools.islice(sys.stdin, count):
x += 1 if '++' in line else -1
print(x)

Why does print('\n') * 100 not work in Python 3.3?

I am attempting to clear lines of code in Python and came across a post at Any way to clear python's IDLE window? on how to do so however when I run the function below in IDLE 3.3 I get the error below. It does however work in version 2.7.3.
ERROR
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
cls()
File "<pyshell#6>", line 2, in cls
print('\n') * 100
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
CODE
def cls():
print('\n') * 100
You probably mean
print('\n' * 100)
When you multiply a string by an int, it is repeated:
>>> 'ha' * 3
'hahaha'
But what you do is multiply the value of print('\n') by 100. But print() doesn't return anything (read: returns None), hence the error: you can't multiply None and int.
In Python 2 there is no difference, because there are no parentheses:
print '\n' * 100
Still, it's interpreted by Python the same way as in Python 3 (and not the same way you seem to iterpret it).

Resources