diff --git a/builtins-c.c b/builtins-c.c index 676f5da3..f003bbb2 100644 --- a/builtins-c.c +++ b/builtins-c.c @@ -51,6 +51,12 @@ */ +#ifdef _MSC_VER +#include +#else +#include +#endif // !_MSC_VER + #include #include #include @@ -139,3 +145,14 @@ void __do_print(const char *format, const char *types, int width, int mask, } fflush(stdout); } + + +int __num_cores() { +#ifdef _MSC_VER + SYSTEM_INFO sysInfo; + GetSystemInfo(&sysInfo); + return sysInfo.dwNumberOfProcessors; +#else + return sysconf(_SC_NPROCESSORS_ONLN); +#endif // !_MSC_VER +} diff --git a/docs/ispc.txt b/docs/ispc.txt index f6415e85..89249914 100644 --- a/docs/ispc.txt +++ b/docs/ispc.txt @@ -91,6 +91,7 @@ Contents: + `Conversions To and From Half-Precision Floats`_ + `Atomic Operations and Memory Fences`_ + `Prefetches`_ + + `System Information`_ + `Low-Level Bits`_ * `Interoperability with the Application`_ @@ -2142,6 +2143,20 @@ These functions are available for all of the basic types in the language--``int8``, ``int16``, ``int32``, ``float``, and so forth. +System Information +------------------ + +A routine is available to find the number of CPU cores available in the +system: + +:: + + int num_cores() + +This value can be useful for adapting the granularity of parallel task +decomposition depending on the number of processors in the system. + + Low-Level Bits -------------- diff --git a/stdlib.ispc b/stdlib.ispc index 0b848422..40b4c9e8 100644 --- a/stdlib.ispc +++ b/stdlib.ispc @@ -568,6 +568,13 @@ static inline uniform int packed_store_active(uniform int a[], uniform int start return __packed_store_active(a, start, vals, __mask); } +/////////////////////////////////////////////////////////////////////////// +// System information + +static inline int num_cores() { + return __num_cores(); +} + /////////////////////////////////////////////////////////////////////////// // Atomics and memory barriers