Ali Onur Cinar
Articles

USING MULTIPLE CPUS
IN COMPAQ TRU64

Programmer may use library functions to bind a process or a thread to a specific CPU. How to use multiple CPUs on Tru64 is briefly explained in the sections above.

CPU schema

Tru64 provides bind_to_cpu (3) library function to give programmer the ability to access multiple processors available in the system. To be able to use those functions, programmer needs to include the following header files inside the code.


     /*
     * Copyright (c) 2002 by Ali Onur Cinar <cinar(a)zdo.com>
     *
     * Desc    : Process binding to different CPUs
     * --
     * System  : Compaq ES45 4@1GHz
     * OS      : Compaq Tru64 Unix 5.1b
     * Compiler: Compaq C V6.4-009 on Compaq Tru64 UNIX V5.1A (Rev. 1885)
     * To build: cc cpu.c -lmach -o cpu
     *
     */

    #include <sys/types.h>
    #include <sys/sysinfo.h>
    #include <sys/resource.h>
    #include <sys/processor.h>
    #include <machine/hal_sysinfo.h>
    #include <cpuset.h>
    #include <unistd.h>

    /* for bit mask manipulation */
    #include <limits.h>    // for size of LONG_BIT
    #define BITMASK(bit)   (1 << (bit % LONG_BIT))
    #define BITSET(a,bit)  (a |= BITMASK(bit))
    #define BITTEST(a,bit) (a & BITMASK(bit))

    int
    main (argc,argv)
      int argc;
      char *argv[];
    {
      int ncpu;                     // number of cpu in the system
      int ccpu;                     // current cpu's number
      struct cpu_info cpu_info;     // cpu state
      int cpu_ids[LONG_BIT];        // array of cpu ids
      unsigned long target;         // target cpu
      int i,j;                      // dummy for calculations

      // get the number of cpu on the system
      getsysinfo(GSI_CPUS_IN_BOX, (caddr_t) &ncpu, sizeof(ncpu), 0, 0);
      printf("Number of CPU : %d\n", ncpu);

      // on which cpu are we running
      getsysinfo(GSI_CURRENT_CPU, (caddr_t) &ccpu, sizeof(ccpu), 0, 0);
      printf("Current CPU   : %d\n", ccpu);

      // get cpu state
      getsysinfo(GSI_CPU_INFO, (caddr_t) &cpu_info, sizeof(cpu_info), 0, 0);

      // cpu clock speed
      printf("CPU Speed     : %d MHz\n", cpu_info.mhz);

      // list the available cpu ids (ncpus is the largest id+1)
      printf("Available CPUs: ");
      for (i=0; i<cpu_info.ncpus; i++)
       if (BITTEST(cpu_info.cpus_present, i))
         printf("%d ", i);

      printf("\n");

      // list only the online cpu ids
      printf("Online CPUs   : ");
      for (i=0,j=-1; i<cpu_info.ncpus; i++)
       if (BITTEST(cpu_info.cpus_running, i)) {
         printf("%d ", i);
         cpu_ids[++j]=i;
       }

      printf("\n");

      // trying changing cpu (if we have more than one cpu)
      for (i=0; i<=j; i++) {
        printf("Trying to switch cpu %d...\n", i);

        // set bitmask
        target=0;
        BITSET(target,i);

        // try to bind to cpu
        if (bind_to_cpu(getpid(),target,BIND_NO_INHERIT)) {
          printf("error binding to cpu %i\n",i);
          exit(1);
        }

        // show the current cpu
        getsysinfo(GSI_CURRENT_CPU, (caddr_t) &ccpu, sizeof(ccpu), 0, 0);
        printf("Current CPU   : %d\n", ccpu);

        sleep(2); // sleep 2 seconds
      }

      return 0; // terminated
    }
 

Your Comments


12/31/69 17:00

Name:
Comment:

Valid XHTML 1.0! Valid CSS! FuseBox Inside This is my Google PageRank. - SmE Rank free service Powered by Scriptme

This page was last updated on Sun January 21 2007 06:55:18 PM