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

Add fallback for when CPU count can't be determined #1214

Merged
merged 4 commits into from
Nov 9, 2018

Conversation

charlesastaylor
Copy link
Contributor

Summary

psutil.cpu_count(logical=False) returns None when the true count can't be determined. #1212 found this happening on a raspberry pi.

nonhermitian
nonhermitian previously approved these changes Nov 4, 2018
Copy link
Contributor

@nonhermitian nonhermitian left a comment

Choose a reason for hiding this comment

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

This looks good.

@charlesastaylor
Copy link
Contributor Author

Should I put something in changelog? Not really sure what warrants going in changelog and what doesn't.

@nonhermitian
Copy link
Contributor

Yeah. Just say you fixed an issue with finding num processes

@charlesastaylor
Copy link
Contributor Author

@nonhermitian Thanks, done. Did you see my question in the issue thread?

Checking psutil.cpu_count on my pi I noticed that cpu_count(logical=False) returns None but cpu_count(logical=True) returns 4. Is it worth trying value of logical=True when logical=False returns None or just default straight to 1?

@nonhermitian
Copy link
Contributor

In setting logical to False, we are looking for only physical cpus. Things like hyperthreads do not greatly contribute to the parallel power, and increase the memory count. So we try to avoid them. Not sure why the arm processor says no physical cpus.

@nonhermitian nonhermitian self-assigned this Nov 5, 2018
@nonhermitian nonhermitian added the bug Something isn't working label Nov 5, 2018
@nonhermitian nonhermitian added this to the 0.7 milestone Nov 5, 2018
@mtreinish
Copy link
Member

I'm able to reproduce psutil() returning None on one of my arm devices (not a raspberry pi, don't have one handy right now). Looking at the psutil code for getting the physical cpu count: https://github.com/giampaolo/psutil/blob/master/psutil/_pslinux.py#L624-L646 it's expecting a certain format from /proc/cpuinfo (what x86 CPUs have) but the arm format is different, and it doesn't have the 'physical id' key that psutil is searching for. So it fails to parse and returns None. Looking at the code for the logical cpu count they had a similar issue there and fixed it by parsing /proc/stat instead. I'll submit a bug to psutil and work on a fix later.

Other than the merge conflict on the changelog, this LGTM. A unit test would be nice, but it's not worth blocking over for something this simple.

@charlesastaylor
Copy link
Contributor Author

@mtreinish Yeah I found the same thing when I looked. I came across this closed issue first and stopped at that I'm afraid!

Will fix merge conflict now. Thought about trying to add test but didn't know how I would do it I'm afraid.

mtreinish
mtreinish previously approved these changes Nov 6, 2018
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

No worries, fwiw you could do something like:

    @mock.patch('platform.system', return_value='Linux')
    @mock.patch('psutil.virtual_memory')
    @mock.patch('psutil.cpu_count', return_value=None)
    def test_local_hardware_None_cpu_count(self, cpu_count_mock, vmem_mock,
                                           platform_mock):
        result = _util.local_hardware_info()
        self.assertEqual(1, result['cpus'])

In test/python/test_util.py. But it's not worth blocking over, we can always add it later too.

@charlesastaylor
Copy link
Contributor Author

@mtreinish Thanks! I had no idea about mock so didn't know where to begin for writing a test.

Added test as suggested with some minor adjustments for linting.

mtreinish
mtreinish previously approved these changes Nov 6, 2018
@mtreinish
Copy link
Member

@charlesastaylor Sigh, well it looks like test_util.py file was removed in #1198 (since it removed the need for the only test that was in there in the patch) so you'll have to just add the file back with only the new test that you're adding here.

Copy link
Contributor

@delapuente delapuente left a comment

Choose a reason for hiding this comment

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

One CPU seems like a good fallback. :p

Thank you.

test/python/test_util.py Outdated Show resolved Hide resolved
*Fallback to 1 when cpu_count returns None

*Create test_util.py and add test

Fixes Qiskit#1212
Copy link
Member

@diego-plan9 diego-plan9 left a comment

Choose a reason for hiding this comment

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

Thanks @charlesastaylor for the neat fix and for the quick adjustments in this high-activity phase! I'm clicking the approve button and merging, but the real credit for the review is to @nonhermitian, @mtreinish and @delapuente - thanks everyone, and looking forward to more contributions!

@diego-plan9 diego-plan9 dismissed delapuente’s stale review November 9, 2018 10:24

Dismissing due to github bureocracy - the changes requested have been performed

@diego-plan9 diego-plan9 merged commit 42d8eaf into Qiskit:master Nov 9, 2018
@charlesastaylor charlesastaylor deleted the cpu-count-fallback branch November 12, 2018 10:30
lia-approves pushed a commit to edasgupta/qiskit-terra that referenced this pull request Jul 30, 2019
…skit#1214)

*Fallback to 1 when cpu_count returns None

*Create test_util.py and add test

Fixes Qiskit#1212
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants