Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26757 Discussions

How to determine which core a program runs on?

Tue_B_
Beginner
268 Views

I have a single threaded program which I would like to run multiple times, and ideally I would like it to start up on any free core - though I realize that may not be possible. I know how to bind the program to a specific core for multi-threaded programs using  SETENVQQ and OpenMP, but that does not seem to work when I only use 1 thread, so how do you bind a single threaded program to a specific core/logicalproccessor?

0 Kudos
1 Solution
JVanB
Valued Contributor II
268 Views

Now, hopefully you're not using OMP because I don't know whether that would play nice with code that asks the OS to reserve a specific CPU for its purposes. What I was thinking of was something like

program affinity
   use IFWIN
   implicit none
   integer, parameter :: dp = kind(1.0d0)
   integer(HANDLE) Process
   integer(DWORD_PTR) AffinityMask
   integer Core
   integer(DWORD) i
   real(dp) x, sum
   integer(BOOL) Retval
   integer(DWORD) iError

   Process = GetCurrentProcess()
   Core = 2 ! Set to run on core #2
   AffinityMask = ishft(1_DWORD_PTR, Core)
   Retval = SetProcessAffinityMask(Process,AffinityMask)
   if(Retval == 0) then
      iError = GetLastError()
      write(*,*) 'Failed with error code ',iError
      stop
   end if
   sum = 0
   do i = huge(i),1,-1
      x = i
      x = 1/x**2
      sum = sum+x
   end do
   write(*,*) huge(i),sum
end program affinity

Now, when I run the above and look in Resource Manager under the CPU tab, I see a spike in usage for CPU 2 and none for the others, so it looks like it is working.

EDIT: If you wanted the process to use CPUs 0, 1, and 2, you could have coded

   AffinityMask = iany(ishft(1_DWORD_PTR, [0,1,2]))

 

View solution in original post

6 Replies
Nikitopoulos__Theodo
268 Views

You can check these articles:

http://www.howtogeek.com/howto/windows-vista/start-an-application-assigned-to-a-specific-cpu-in-wind...
http://www.howtogeek.com/121775/how-to-force-windows-applications-to-use-a-specific-cpu/

I'm not sure if there is a windows api to change the affinity from within the program. Maybe someone else can help on this.

 

JVanB
Valued Contributor II
268 Views

Have you looked at SetProcessAffinityMask?

Tue_B_
Beginner
268 Views

Repeat Offender wrote:

Have you looked at SetProcessAffinityMask?

I have now, but I'm still not exactly sure whether this can do what I want. What I'm currently doing is using:

SETENVQQ("OMP_PLACES={0},{1},{2}")

But this does not get respected if I'm only using 1 thread, and so I'm not really sure how I would use SetProcessAffinityMask.

 

 

JVanB
Valued Contributor II
269 Views

Now, hopefully you're not using OMP because I don't know whether that would play nice with code that asks the OS to reserve a specific CPU for its purposes. What I was thinking of was something like

program affinity
   use IFWIN
   implicit none
   integer, parameter :: dp = kind(1.0d0)
   integer(HANDLE) Process
   integer(DWORD_PTR) AffinityMask
   integer Core
   integer(DWORD) i
   real(dp) x, sum
   integer(BOOL) Retval
   integer(DWORD) iError

   Process = GetCurrentProcess()
   Core = 2 ! Set to run on core #2
   AffinityMask = ishft(1_DWORD_PTR, Core)
   Retval = SetProcessAffinityMask(Process,AffinityMask)
   if(Retval == 0) then
      iError = GetLastError()
      write(*,*) 'Failed with error code ',iError
      stop
   end if
   sum = 0
   do i = huge(i),1,-1
      x = i
      x = 1/x**2
      sum = sum+x
   end do
   write(*,*) huge(i),sum
end program affinity

Now, when I run the above and look in Resource Manager under the CPU tab, I see a spike in usage for CPU 2 and none for the others, so it looks like it is working.

EDIT: If you wanted the process to use CPUs 0, 1, and 2, you could have coded

   AffinityMask = iany(ishft(1_DWORD_PTR, [0,1,2]))

 

View solution in original post

Tue_B_
Beginner
268 Views

Repeat Offender wrote:

Now, hopefully you're not using OMP because I don't know whether that would play nice with code that asks the OS to reserve a specific CPU for its purposes. What I was thinking of was something like

program affinity
   use IFWIN
   implicit none
   integer, parameter :: dp = kind(1.0d0)
   integer(HANDLE) Process
   integer(DWORD_PTR) AffinityMask
   integer Core
   integer(DWORD) i
   real(dp) x, sum
   integer(BOOL) Retval
   integer(DWORD) iError

   Process = GetCurrentProcess()
   Core = 2 ! Set to run on core #2
   AffinityMask = ishft(1_DWORD_PTR, Core)
   Retval = SetProcessAffinityMask(Process,AffinityMask)
   if(Retval == 0) then
      iError = GetLastError()
      write(*,*) 'Failed with error code ',iError
      stop
   end if
   sum = 0
   do i = huge(i),1,-1
      x = i
      x = 1/x**2
      sum = sum+x
   end do
   write(*,*) huge(i),sum
end program affinity

Now, when I run the above and look in Resource Manager under the CPU tab, I see a spike in usage for CPU 2 and none for the others, so it looks like it is working.

EDIT: If you wanted the process to use CPUs 0, 1, and 2, you could have coded

   AffinityMask = iany(ishft(1_DWORD_PTR, [0,1,2]))

 

 

Thank you, this did exactly what I wanted!

Is there any resource I could read about how it actually work, or what other commands I would be able to do with these kinds of things? I haven't been able to find anything about this, which is probably because I'm searching for this from a Fortran point of view.

JVanB
Valued Contributor II
268 Views

I don't know what to tell you further except to read the documentation provided in MSDN. For example, just search for

SetProcessAffinityMask msdn

And you will get all the information that I have. More general advice on using Google: try to state your query as an English sentence, including all or most of the keywords that you are really searching for, like you would be asking a person. Google thinks it's smarter than you and anthropomorphizing it in this way confirms its attitude and so gets you better service.

 

Reply