1

Environment

  • Ubuntu 20.04.3 LTS
  • Intel Core i5-7300HQ CPU @ 2.50GHz × 4 (4 cores)
  • .Net Core version: 5.0.402

Problem

Print the CPU core number (not the number of available cores) that an item is running on.

Code

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Test {
    class Program {
        static void Main(string[] args) {
            Run();
        }

        static void Run() {
            string fileDirectory = "/path/to/files/";

            List<string> symbols = new List<string> {
                "AAPL",
                "AMZ",
                "NICK",
                "ZEUS"
            };

            ParallelOptions parallelOptions = new ParallelOptions {MaxDegreeOfParallelism = Environment.ProcessorCount};

            Console.WriteLine("parallelOptions.MaxDegreeOfParallelism = " + parallelOptions.MaxDegreeOfParallelism);

            Parallel.ForEach(symbols, parallelOptions, symbol => {
                // Pass symbol filepath to ProcessFile.
                ProcessFile(String.Format("{0}{1}{2}", fileDirectory, symbol, ".csv"));
            });
        }

        static void ProcessFile(string filePath){
            Console.WriteLine("Processing " + filePath);
        }
    }
}

Current Output

parallelOptions.MaxDegreeOfParallelism = 4
Processing /path/to/files/AAPL.csv
Processing /path/to/files/AMZ.csv
Processing /path/to/files/ZEUS.csv
Processing /path/to/files/NICK.csv

Desired Output

parallelOptions.MaxDegreeOfParallelism = 4
Processing /path/to/files/AAPL.csv on CPU Core 1
Processing /path/to/files/AMZ.csv on CPU Core 4
Processing /path/to/files/ZEUS.csv on CPU Core 2
Processing /path/to/files/NICK.csv on CPU Core 3

Question

What is the correct way to get the CPU core number (not the number of available cores)? Edit: ideally looking for a .Net Core class/method instead of calling an external OS command (Top, PS, etc.).

fire_water
  • 1,380
  • 1
  • 19
  • 33
  • 1
    Does this answer your question? [Is there way to get which core is used by the thread?](https://stackoverflow.com/questions/51230802/is-there-way-to-get-which-core-is-used-by-the-thread) – 500 - Internal Server Error Oct 28 '21 at 22:03
  • Great username, lol. Thanks for the link. Although it doesn't directly answer the question (hoping for a .Net Core method/class instead of calling an external command) it might be the only option if nothing else is available. I'll dig deeper into that link. – fire_water Oct 28 '21 at 22:07
  • https://stackoverflow.com/questions/49284638/how-to-get-the-core-processor-id-of-executing-thread – Dmitry Bychenko Oct 28 '21 at 22:12
  • 2
    Does `System.Threading.Thread.GetCurrentProcessorId()` not work for you? See [docs](https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.getcurrentprocessorid?view=net-5.0). – G Wimpassinger Oct 28 '21 at 22:22
  • 1
    Is this what you need? [getcpu](http://manpages.ubuntu.com/manpages/trusty/man2/getcpu.2.html) – NetMage Oct 28 '21 at 22:35
  • 3
    A dirty little secret: the os and cpu can reschedule threads on to different cores at anytime – Daniel A. White Oct 29 '21 at 01:53
  • Thanks, guys. I ended up using `System.Threading.Thread.GetCurrentProcessorId()` (thanks @GWimpassinger) realizing the OS and CPU can reschedule threads on to different cores at anytime (thanks @Daniel A. White). – fire_water Oct 29 '21 at 16:02

0 Answers0