C


C: Reading Complex Numbers

A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit, satisfying the equation i*i=−1. In this expression, a is the real part and b is the imaginary part of the complex number.

From wikipedia

The following code, will open a file where each line contains one complex number, read the number and store it into the appropriate variables.


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

  const char *filename = argv[1];
  FILE *fin = fopen(filename, "r");

  // Making sure we managed to open the file
  if (fin != NULL) {

    //Initially this pointer will hold the whole line, eventually the data for the second Component will be removed
    char *first_value = NULL;
    size_t length_of_buffer = 0;
    ssize_t bytes_read;

    // Read Line by line until end of file:
    // If *line is set to NULL and *length_of_buffer is set 0 before the call, then getline() will allocate a buffer for storing the line.
    // This buffer should be freed by the user program even if getline() failed.
    while ((bytes_read = getline(&first_value, &length_of_buffer, fin)) != -1) {
      printf("Data = '%s' : Length = %zu\n", first_value, bytes_read);

      // Create char string to store the second Component
      char *second_value;
      // We are a bit wasteful here but it makes the code simpler
      second_value = (char *) malloc(bytes_read * sizeof(char));

      // Create Imaginary flag to distinguish real values from imaginary values - One for each Component
      char imaginary_flag[2] = {0, 0};

      // Initialize Iterators
      // i: for pointing to characters of current line
      // j: for pointing to characters in first Component
      // k: for pointing to characters in second Component
      int i, j = 0, k = 0;
      // Flag to indicate that first character if the first Component was found - This helps distinguish between first and second Component
      char met_a_number = 0;
      // Flag to indicate first Component ends and we should process the second Component
      char copy_to_second_value = 0;
      // To store the previous character value, we use it to check that when a sign is found, it does not belong to an exponential
      char previous_character = '\0';

      for (i = 0; i < bytes_read; i++) {
        // Initialize current character being processed/evaluated from the line
        const char current_character = first_value[i];

        // Find the 'j' character that shows that this number is imaginary, then drop the character
        if (current_character == 'j') {
          if (copy_to_second_value == 0) {
            // First Component is the imaginary part
            imaginary_flag[0] = 1;
          } else {
            // Second Component is the imaginary part
            imaginary_flag[1] = 1;
          }
          continue;
        }

        //We check that the current character is useful to us
        if (!(current_character == ' ' || current_character == '(' || current_character == ')')) {
          // If current character still part of first Component [based on flag]
          if (copy_to_second_value == 0) {
            // Check when second Component begins
            // When we find one of the sign characters and it is not part of an exponential, then we switch to the second Component
            // Exclude if previous character was "e" because after "e" a sign is followed
            if (current_character == '-' || current_character == '+') {
              if (met_a_number == 1 && previous_character != 'e') {
                // Update Flag to indicate that second Component/Value begins
                copy_to_second_value = 1;
                // Terminate first Component/Value of current Line
                first_value[i] = '\0';
                second_value[k++] = current_character;
              } else {
                // Update First Component with the current character value
                first_value[j++] = current_character;
              }
            }
              // If current character still part of the first Component
            else {
              // Update First Component with the current character value
              first_value[j++] = current_character;
              // A valid character of a number was found, so we are processing a number.
              // If this is the first time it happens, we are processing the first Component
              met_a_number = 1;
            }
          }
            // If current character part of second Component [based on flag]
          else {
            second_value[k++] = current_character;
          }
        }
          // If current character belongs in one of the garbage characters
        else {
          // If current character is garbage number and we already met a number
          // then we start copying to the second Component
          if (met_a_number == 1) {
            copy_to_second_value = 1;
            first_value[i] = '\0';
          }
        }

        // Store current_character before reading the next
        previous_character = current_character;
      }

      // Properly terminate first Component
      if (first_value[j - 1] == '\n') {
        first_value[j - 1] = '\0';
      } else {
        first_value[j] = '\0';
      }

      // Properly terminate second Component
      second_value[k - 1] = '\0';

      // To store the first and second Components in float format
      double numbers[2];

      // Convert to float and Print first Component
      sscanf(first_value, "%lf", &(numbers[0]));
      printf("%.f = '%s' is imaginary = %s\n", numbers[0], first_value, imaginary_flag[0] == 0 ? "FALSE" : "TRUE");

      // Convert to float and Print second Component if it exists
      if (copy_to_second_value == 1) {
        sscanf(second_value, "%lf", &(numbers[1]));
        printf("%.f = '%s' is imaginary = %s\n", numbers[1], second_value, imaginary_flag[1] == 0 ? "FALSE" : "TRUE");
      }

      free(second_value);

      double real = 0, imaginary = 0;
      if (copy_to_second_value == 1) {
        if (imaginary_flag[0] == imaginary_flag[1]) {
          fprintf(stderr, "Invalid input line.\n");
          if (imaginary_flag[0] == 0) {
            fprintf(stderr, "None of the components is imaginary\n");
          } else {
            fprintf(stderr, "Both components are imaginary.\n");
          }
          continue;
        }
        if (imaginary_flag[0] == 0) {
          real = numbers[0];
          imaginary = numbers[1];
        } else {
          real = numbers[1];
          imaginary = numbers[0];
        }

      } else {
        if (imaginary_flag[0] == 0) {
          real = numbers[0];
        } else {
          imaginary = numbers[0];
        }
      }
      printf("Real part: '%f'\tImaginary part: '%f'\n", real, imaginary);
      printf("\n");
    }
    free(first_value);
    fclose(fin);
  } else {
    fprintf(stderr, "Failed to open file '%s'\n", filename);
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}

This application will only perform some basic checks on the input, a lot of invalid data can pass as valid..
What this application expects though is the following:

  • On each line, there can be either one or two numbers.
  • The numbers can be encapsulated in parenthesis ()
  • The first number can have a sign or not
  • There might be space between the characters or not
  • There might be space between the sign characters and the numbers
  • The imaginary part of the number will have the character j either before the number or after, attached to the number

C/C++: Set Affinity to threads Example Code 1

The following code sets the affinity of each pthread to a different and specific CPU core.

The selection is made with the variable speid (that is user defined) and contains a number from 0 to (CPU NUMBER – 1).

int s, j;
cpu_set_t cpuset;
pthread_t thread;

thread = pthread_self();

/* Set affinity mask to include CPUs 0 to 7 */

CPU_ZERO(&cpuset);
CPU_SET(speid, &cpuset);

s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (s != 0) {
    handle_error_en(s, "pthread_setaffinity_np");
}

/* Check the actual affinity mask assigned to the thread */
s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (s != 0) {
    handle_error_en(s, "pthread_getaffinity_np");
}

printf("Set returned by pthread_getaffinity_np() contained:\n");
for (j = 0; j < CPU_SETSIZE; j++) {
    if (CPU_ISSET(j, &cpuset)) {
        fprintf(stderr,"%d CPU %d\n",speid, j);
    }
}

This code also verifies that the affinity set was successful.

Please note that you can use CPU_SET(core_id, &cpuset); multiple times, with different values for the variable core_id. This way you instruct the OS that it can move your thread to any of those available cores for execution and not limit it to just one.

Full example

Below you will find a full working example. This code will create 4 pthreads, assign each of them to a different CPU core, test that the affiliation was successful and then wait for all the threads to terminate and return their output in the form of a string.

Full source code available here [download id=”2370″]

#include <stdio.h>
#include <stdlib.h>
#define __USE_GNU
#include <sched.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>

// The <errno.h> header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate what went wrong.
#define print_error_then_terminate(en, msg) \
  do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
#define print_perror_then_terminate(msg) \
  do { perror(msg); exit(EXIT_FAILURE); } while (0)

  struct thread_info {

    pthread_t thread_id; // ID returned by pthread_create()
    int core_id; // Core ID we want this pthread to set its affinity to
  };

#define SUCCESS_MSG "Successfully set thread %lu to affinity to CPU %d\n"
#define FAILURE_MSG "Failed to set thread %lu to affinity to CPU %d\n"


void * thread_camper(void *arg) {

  struct thread_info *thread_info = arg;

  const pthread_t pid = pthread_self();
  const int core_id = thread_info->core_id;

  // cpu_set_t: This data set is a bitset where each bit represents a CPU.
  cpu_set_t cpuset;
  // CPU_ZERO: This macro initializes the CPU set set to be the empty set.
  CPU_ZERO(&cpuset);
  // CPU_SET: This macro adds cpu to the CPU set set.
  CPU_SET(core_id, &cpuset);

  // pthread_setaffinity_np: The pthread_setaffinity_np() function sets the CPU affinity mask of the thread thread to the CPU set pointed to by cpuset. If the call is successful, and the thread is not currently running on one of the CPUs in cpuset, then it is migrated to one of those CPUs.
  const int set_result = pthread_setaffinity_np(pid, sizeof(cpu_set_t), &cpuset);
  if (set_result != 0) {

    print_error_then_terminate(set_result, "pthread_setaffinity_np");
  }

  // Check what is the actual affinity mask that was assigned to the thread.
  // pthread_getaffinity_np: The pthread_getaffinity_np() function returns the CPU affinity mask of the thread thread in the buffer pointed to by cpuset.
  const int get_affinity = pthread_getaffinity_np(pid, sizeof(cpu_set_t), &cpuset);
  if (get_affinity != 0) {

    print_error_then_terminate(get_affinity, "pthread_getaffinity_np");
  }

  char *buffer;
  // CPU_ISSET: This macro returns a nonzero value (true) if cpu is a member of the CPU set set, and zero (false) otherwise. 
  if (CPU_ISSET(core_id, &cpuset)) {

    const size_t needed = snprintf(NULL, 0, SUCCESS_MSG, pid, core_id);
    buffer = malloc(needed);
    snprintf(buffer, needed, SUCCESS_MSG, pid, core_id);
  } else {

    const size_t needed = snprintf(NULL, 0, FAILURE_MSG, pid, core_id);
    buffer = malloc(needed);
    snprintf(buffer, needed, FAILURE_MSG, pid, core_id);
  }

  return buffer;
}

int main(int argc, char *argv[]) {

  // Initialize thread creation attributes
  pthread_attr_t attr;
  const int attr_init_result = pthread_attr_init(&attr);
  if (attr_init_result != 0) {
      print_error_then_terminate(attr_init_result, "pthread_attr_init");
  }

  // We will set the stack size limit to is 1 MB (0x100000 bytes)
  const int stack_size = 0x100000;
  const int setstacksize_result = pthread_attr_setstacksize(&attr, stack_size);
  if (setstacksize_result != 0) {
    print_error_then_terminate(setstacksize_result, "pthread_attr_setstacksize");
  }

  const int num_threads = 4;
  // Allocate memory for pthread_create() arguments
  struct thread_info *thread_info = calloc(num_threads, sizeof(struct thread_info));
  if (thread_info == NULL) {
      print_perror_then_terminate("calloc");
  }


  // Create the threads and initialize the core_id argument, which will be used to set the thread to the specific CPU core.
  // For example, we want the first pthread to camp on the first CPU core which has the ID 0. So we pass the value 0 to its core_id.
  int tnum;
  for (tnum = 0; tnum < num_threads; tnum++) {

    thread_info[tnum].core_id = tnum;
    // The pthread_create() call stores the thread ID into corresponding element of thread_info[]
    const int create_result = pthread_create(&thread_info[tnum].thread_id, &attr, &thread_camper, &thread_info[tnum]);
    if (create_result != 0) {
      print_error_then_terminate(create_result, "pthread_create");
    }
  }

  // Destroy the thread attributes object, since it is no longer needed
  const int destroy_result = pthread_attr_destroy(&attr);
  if (destroy_result != 0) {
   
   print_error_then_terminate(destroy_result, "pthread_attr_destroy");
  }

  // Now join with each thread, and display its returned value
  for (tnum = 0; tnum < num_threads; tnum++) {
    void *res;
    const int join_result = pthread_join(thread_info[tnum].thread_id, &res);
    if (join_result != 0) {
      print_error_then_terminate(join_result, "pthread_join");
    }

    printf("Joined with thread %d; returned value was %s\n", thread_info[tnum].core_id, (char *) res);
    free(res); // Free memory allocated by thread
  }

  free(thread_info);
  return 0;
}

To compile we used the following command


gcc -Wall -pthread  affinity_pthread.c -o  affinity_pthread;

Full source code available here [download id=”2370″]

For a full example that sets affinity for single threaded applications please visit this link.


Debugging Trick Using Variadic Macros in C and C++

Following you can find a very practical trick that allows you to enable/disable all prints to the screen with as little effort as possible while not dramatically increasing the overall code size to do this.

For C:  Place on the top of your code or on a header file the following:

#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
    #define XDEBUG(...) printf(__VA_ARGS__)
#else
    #define XDEBUG(...)  /**/
#endif

and then inside the code whenever you want to print some debug information do as follows:

XDEBUG("Max Weight %d, Total Trips %d\n", minMax, trips);

For C++:  Place on the top of your code or on a header file the following:

#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
    #define XDEBUG cout
#else
    #define XDEBUG if(0) cerr
#endif

and then inside the code whenever you want to print some debug information do as follows:

XDEBUG << "We got " << disks << " disks\n";