Drag0
Drag0

Reputation: 8918

High performance computing pthread parameters - c++

I'm working on a project where I need to use multiple threads using pthread (C++). I have a question:

What is the best pthread parameter configuration setting for when I want to do some high performance computing in that thread without too much other threads interrupting it?

Currently I'm using something like this:

pthread_t thread;

struct sched_param param;
int sched = SCHED_FIFO;
memset(&param, 0, sizeof(sched_param));

// Now I set priority to max value (sched_get_priority_max() returns that value)
param.sched_priority = sched_get_priority_max();

// Create my thread
pthread_create(&thread, NULL, (void *) &hard_number_crunching, (void *) &structure_passed_to_thread);

// finally I set parameters to thread
pthread_setschedparam(&thread, sched, &param);

I was switching "int sched" between SCHED_FIFO and SCHED_RR, but it didn't help me much. Is it possible to force this thread to run longer on the CPU than it is at the moment?

Upvotes: 2

Views: 1128

Answers (3)

Drag0
Drag0

Reputation: 8918

Both zr. and mark answers helped me. I also found a way to set affinity for individual thread. Check this link to see how: pthread_attr_setaffinity_np()

Thanks everyone who helped.

Upvotes: 0

zr.
zr.

Reputation: 7788

If you are creating one thread per core, you probably want to set the thread's affinity to prevent it from roaming between cores. This usually improves the performance by ensuring that each thread remains close to its cached data. See:

int sched_setaffinity(pid_t pid,size_t cpusetsize,cpu_set_t *mask);

Note: you should not set the affinity if you are creating more threads than cores! This could cause all kind of crazy things to happen, deadlocks to mention one.

Upvotes: 2

mark
mark

Reputation: 5469

Let's say your thread is going to take X cycles. It doesn't matter what the priority is, or what else the system is doing, it's still going to take X cycles. If the system is not busy doing other things, your thread will run nearly continuously until it finishes. If the system is busy, your thread will be stopped, perhaps even frequently, to let other things run. Eventually your thread will still get its X cycles. Note that for your purposes here, there are two types of priorities... thread priorities (the priority of your one thread versus the priority of your other threads), and process priorities (the priority of your process/program versus the priority of other processes/programs/operating-system). You're setting the thread priority. Do you have other threads of equal or higher priority competing with this one? Are there system processes of higher priority competing with yours? If the answer is no, you'll pretty much get the CPU to yourself. Also note, if you have multiple processors and/or cores, your thread may not run well on more than one, so your overall system processing may not be 100% if you haven't divided up your tasks well.

Upvotes: 0

Related Questions