Skip to content

Instantly share code, notes, and snippets.

@crabba
Last active February 28, 2024 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crabba/696cface4246718d93e1a079ca3fb106 to your computer and use it in GitHub Desktop.
Save crabba/696cface4246718d93e1a079ca3fb106 to your computer and use it in GitHub Desktop.
2402-pcluster-threads
#include <chrono>
#include <iostream>
#include <thread>
#include <unistd.h>
#include <vector>
void whoami(int i) {
int sleep_ms = rand() % 5000;
std::thread::id this_id = std::this_thread::get_id();
std::cout << "Thread " << i << ": " << this_id << " hello" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
std::cout << "Thread " << i << ": " << this_id << " goodbye" << std::endl;
}
int getThreads(void) {
const auto processor_count = std::thread::hardware_concurrency();
std::cout << processor_count << " threads available" << std::endl;
return processor_count;
}
float getMemory() {
long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
float system_memory_gib = (pages * page_size) / std::pow(1024, 3);
std::cout << "System memory GiB: " << std::setprecision(3) << system_memory_gib << std::endl;
return system_memory_gib;
}
int main(int argc, char const *argv[]) {
int nthreads;
std::vector<std::thread> threads = {};
getMemory();
nthreads = getThreads();
for (int i = 0; i < nthreads; i++) {
threads.push_back(std::thread(whoami, i));
}
for (int i = 0; i < nthreads; i++) {
threads[i].join();
}
return 0;
}
#!/bin/bash
#SBATCH --job-name="param_boot_r5a-2xl"
#SBATCH --ntasks=1
#SBATCH --time=96:00:00
#SBATCH --cpus-per-task=8
#SBATCH --ntasks-per-node=1
#SBATCH --exclusive
#SBATCH --output="param_boot_%A_%a.log"
#SBATCH --error="param_boot_%A_%a.err"
#SBATCH --array=[1-10]
#SBATCH --partition=r5a-2xl
mpirun ./multithread_01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment