Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

Thursday, December 27, 2012

Patch to make xz use all the processor cores

--- xz-5.1.2alpha/src/xz/hardware.c    2012-07-05 01:51:14.000000000 +0800
+++ xz-5.1.2alpha-mt/src/xz/hardware.c    2012-12-27 14:52:25.368253769 +0800
@@ -13,13 +13,13 @@
 #include "private.h"
 #include "tuklib_cpucores.h"


 /// Maximum number of worker threads. This can be set with
 /// the --threads=NUM command line option.
-static uint32_t threads_max = 1;
+static uint32_t threads_max = 0;

 /// Memory usage limit for compression
 static uint64_t memlimit_compress;

 /// Memory usage limit for decompression
 static uint64_t memlimit_decompress;
@@ -45,12 +45,15 @@
 }


 extern uint32_t
 hardware_threads_get(void)
 {
+    if (0 == threads_max)
+        hardware_threads_set(0);
+
     return threads_max;
 }


 extern void
 hardware_memlimit_set(uint64_t new_memlimit,

Wednesday, December 26, 2012

Detect Number of Processor Cores

$ cat a.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main()
{
    long cpus;
    char *envconfig;

    cpus = sysconf(_SC_NPROCESSORS_CONF);
    printf("Processors configured: %d\n", cpus);
    cpus = sysconf(_SC_NPROCESSORS_ONLN);
    printf("Processors online:     %d\n", cpus);
    envconfig = getenv("PARALLEL");
    if (NULL == envconfig) {
        printf("PARALLEL not defined\n");
    } else {
        printf("PARALLEL=%s (%d)\n", envconfig, strlen(envconfig));
    }
    if ((NULL != envconfig) && (0 == strcmp("OFF", envconfig))) {
        printf("PARALLEL=OFF\n");
    } else {
        printf("PARALLEL=ON\n");
    }

    return 0;
}

$ gcc a.c
$ ./a.out
Processors configured: 2
Processors online:     2
PARALLEL= (0)
PARALLEL=ON

$ export PARALLEL=OFF
$ ./a.out
Processors configured: 2
Processors online:     2
PARALLEL=OFF

$ export PARALLEL=ON
$ ./a.out

Processors configured: 2
Processors online:     2
PARALLEL=ON (2)
PARALLEL=ON

$ export PARALLEL=
$ ./a.out
Processors configured: 2
Processors online:     2
PARALLEL= (0)
PARALLEL=ON