<commit_before>#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif

#define DATA_TYPE unsigned long

#define SOCKET_NAME "pir.socket"

////////////////////////////////////////////////////////////////////////////////
//
// PIR Server.
//
// Communication occurs via a socket. several commands are present.
// 1. read. expects an input pir vector.
// 2. configure. sets parameters for batching of groups.
// 3. write. updates the database.
////////////////////////////////////////////////////////////////////////////////
int cell_length;
int cell_count;
int batch_size;
size_t workgroup_size;
char* invector;
DATA_TYPE* database;
DATA_TYPE* output;
cl_context context;                 // compute context
cl_command_queue commands;          // compute command queue
cl_program program;                 // compute program
cl_kernel kernel;                   // compute kernel
cl_mem gpu_db;                      // device memory used for the database
cl_mem gpu_input;
cl_mem gpu_output;

int configure(int, int, int, int);
int do_write(DATA_TYPE*);
void listDevices();
DATA_TYPE* do_read(char*);


static volatile char* socketpath;
void intHandler(int dummy) {
  unlink((char*)socketpath);
  exit(1);
}


int main(int argc, char** argv)
{
    int err;
    int c;
    int listonly = 0;
    socketpath = SOCKET_NAME;
    int deviceid = 0;
    while ((c = getopt(argc, argv, "s:d:l")) != -1) {
      switch (c) {
        case 'l':
          listonly = 1;
          break;
        case 'd':
          deviceid = atoi(optarg);
          break;
        case 's':
          socketpath = optarg;
          break;
        default:
          fprintf(stderr, "Usage: %s [-l] [-d <device id>] [-s <socket>].\n",
              argv[0]);
          return 1;
      }
    }

    if (listonly) {
      listDevices();
      return 1;
    }

    signal(SIGINT, intHandler);

    int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
    int client_sock;
    if (socket_fd == -1) {
      printf("Error: Failed to create sockets!\n");
      return EXIT_FAILURE;
    }

    // Bind the socket for communication.
    unlink((char*)socketpath);
    struct sockaddr_un socket_name;
    memset(&socket_name, 0, sizeof(struct sockaddr_un));
    socket_name.sun_family = AF_UNIX;
    strncpy(socket_name.sun_path, (char*)socketpath, sizeof(socket_name.sun_path) - 1);
    err = bind(socket_fd, (const struct sockaddr *) &socket_name, sizeof(struct sockaddr_un));
    if (err == -1) {
      printf("Error: Failed to bind socket!\n");
      return EXIT_FAILURE;
    }

    err = listen(socket_fd, 1);
    if (err == -1) {
      printf("Error: Failed to listen on socket!\n");
      return EXIT_FAILURE;
    }


    char next_command;
    int readamount;
    int ret;
    int dbhndl;
    int newdbhndl;
    int configuration_params[3];
    for (;;) {
      client_sock = accept(socket_fd, NULL, NULL);
      if (client_sock == -1) {
        printf("Error: failed to accept client on listening socket!\n");
        return EXIT_FAILURE;
      }
      for(;;) {
        /* Wait for next data packet. */
        ret = read(client_sock, &next_command, 1);
        if (ret == -1 || ret == 0) {
          printf("Client disconnected.\n");
          break;
        }
        if (next_command == '1') { //read
          readamount = 0;
          while (readamount < cell_count * batch_size / 8) {
            ret = read(client_sock, invector, cell_count * batch_size / 8 - readamount);
            if (ret == -1) {
              printf("Failed to read configuration.\n");
              break;
            }
            readamount += ret;
          }
          output = do_read(invector);
          readamount = 0;
          while (readamount < cell_length * batch_size) {
            ret = write(client_sock, output, cell_length * batch_size - readamount);
            if (ret == -1) {
              printf("Failed to write response.\n");
              break;
            }
            readamount += ret;
          }
        } else if (next_command == '2') { //configure
          ret = read(client_sock, configuration_params, 3*sizeof(int));
          if (ret == -1) {
            printf("Failed to read configuration.\n");
            break;
          }
          configure(deviceid, configuration_params[0], configuration_params[1], configuration_params[2]);
        } else if (next_command == '3') { // write
          ret = read(client_sock, &newdbhndl, sizeof(newdbhndl));
          if (ret == -1) {
            printf("Failed to learn shm id.\n");
            break;
          }
          if (newdbhndl != dbhndl && dbhndl != 0) {
            shmdt(database);
          }
          if (newdbhndl != dbhndl) {
            database = shmat(dbhndl, NULL, SHM_RDONLY);
            if (database == (void*)-1) {
              printf("Failed to open shm ptr: %d.\n", errno);
              break;
            }
          }
          do_write(database);
          write(client_sock, "ok", 2);
        } else {
          printf("Unexpected command: %c\n", next_command);
          break;
        }
      }
    }
}

void listDevices() {
  int i, num;
  char buf[256];
  cl_device_id device_ids[10];
  cl_platform_id cl_platform;<commit_msg>pird logic on sync's for shared memory<commit_after>#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif

#define DATA_TYPE unsigned long

#define SOCKET_NAME "pir.socket"

////////////////////////////////////////////////////////////////////////////////
//
// PIR Server.
//
// Communication occurs via a socket. several commands are present.
// 1. read. expects an input pir vector.
// 2. configure. sets parameters for batching of groups.
// 3. write. updates the database.
////////////////////////////////////////////////////////////////////////////////
int cell_length;
int cell_count;
int batch_size;
size_t workgroup_size;
char* invector;
DATA_TYPE* database;
DATA_TYPE* output;
cl_context context;                 // compute context
cl_command_queue commands;          // compute command queue
cl_program program;                 // compute program
cl_kernel kernel;                   // compute kernel
cl_mem gpu_db;                      // device memory used for the database
cl_mem gpu_input;
cl_mem gpu_output;

int configure(int, int, int, int);
int do_write(DATA_TYPE*);
void listDevices();
DATA_TYPE* do_read(char*);


static volatile char* socketpath;
void intHandler(int dummy) {
  unlink((char*)socketpath);
  exit(1);
}


int main(int argc, char** argv)
{
    int err;
    int c;
    int listonly = 0;
    socketpath = SOCKET_NAME;
    int deviceid = 0;
    while ((c = getopt(argc, argv, "s:d:l")) != -1) {
      switch (c) {
        case 'l':
          listonly = 1;
          break;
        case 'd':
          deviceid = atoi(optarg);
          break;
        case 's':
          socketpath = optarg;
          break;
        default:
          fprintf(stderr, "Usage: %s [-l] [-d <device id>] [-s <socket>].\n",
              argv[0]);
          return 1;
      }
    }

    if (listonly) {
      listDevices();
      return 1;
    }

    signal(SIGINT, intHandler);

    int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
    int client_sock;
    if (socket_fd == -1) {
      printf("Error: Failed to create sockets!\n");
      return EXIT_FAILURE;
    }

    // Bind the socket for communication.
    unlink((char*)socketpath);
    struct sockaddr_un socket_name;
    memset(&socket_name, 0, sizeof(struct sockaddr_un));
    socket_name.sun_family = AF_UNIX;
    strncpy(socket_name.sun_path, (char*)socketpath, sizeof(socket_name.sun_path) - 1);
    err = bind(socket_fd, (const struct sockaddr *) &socket_name, sizeof(struct sockaddr_un));
    if (err == -1) {
      printf("Error: Failed to bind socket!\n");
      return EXIT_FAILURE;
    }

    err = listen(socket_fd, 1);
    if (err == -1) {
      printf("Error: Failed to listen on socket!\n");
      return EXIT_FAILURE;
    }


    char next_command;
    int readamount;
    int ret;
    int dbhndl;
    int newdbhndl;
    int configuration_params[3];
    for (;;) {
      client_sock = accept(socket_fd, NULL, NULL);
      if (client_sock == -1) {
        printf("Error: failed to accept client on listening socket!\n");
        return EXIT_FAILURE;
      }
      for(;;) {
        /* Wait for next data packet. */
        ret = read(client_sock, &next_command, 1);
        if (ret == -1 || ret == 0) {
          printf("Client disconnected.\n");
          break;
        }
        if (next_command == '1') { //read
          readamount = 0;
          while (readamount < cell_count * batch_size / 8) {
            ret = read(client_sock, invector, cell_count * batch_size / 8 - readamount);
            if (ret == -1) {
              printf("Failed to read configuration.\n");
              break;
            }
            readamount += ret;
          }
          output = do_read(invector);
          readamount = 0;
          while (readamount < cell_length * batch_size) {
            ret = write(client_sock, output, cell_length * batch_size - readamount);
            if (ret == -1) {
              printf("Failed to write response.\n");
              break;
            }
            readamount += ret;
          }
        } else if (next_command == '2') { //configure
          ret = read(client_sock, configuration_params, 3*sizeof(int));
          if (ret == -1) {
            printf("Failed to read configuration.\n");
            break;
          }
          configure(deviceid, configuration_params[0], configuration_params[1], configuration_params[2]);
        } else if (next_command == '3') { // write
          ret = read(client_sock, &newdbhndl, sizeof(newdbhndl));
          if (ret == -1) {
            printf("Failed to learn shm id.\n");
            break;
          }
          if (newdbhndl != dbhndl && dbhndl != 0) {
            shmdt(database);
          }
          if (newdbhndl != dbhndl) {
            dbhndl = newdbhndl;
            database = shmat(dbhndl, NULL, SHM_RDONLY);
            if (database == (void*)-1) {
              printf("Failed to open shm ptr: %d.\n", errno);
              break;
            }
          }
          do_write(database);
          write(client_sock, "ok", 2);
        } else {
          printf("Unexpected command: %c\n", next_command);
          break;
        }
      }
    }
}

void listDevices() {
  int i, num;
  char buf[256];
  cl_device_id device_ids[10];
  cl_platform_id cl_platform;
{'fraction_non_alphanumeric': 0.15408890091064828, 'fraction_numerical': 0.008295013975295284, 'mean_word_length': 1.881038961038961, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5027497', 'n_tokens_mistral': 3521, 'n_tokens_neox': 3440, 'n_words': 909}