<commit_before>#include <CoreGraphics/CoreGraphics.h>
#include <iostream>
#include <spdlog/spdlog.h>

namespace {
CFMachPortRef eventtap_;

CGEventRef callback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon) {
  switch (type) {
  case kCGEventKeyDown:
    std::cout << "kCGEventKeyDown" << std::endl;
    break;
  case kCGEventKeyUp:
    std::cout << "kCGEventKeyUp" << std::endl;
  default:
    std::cout << "callback:" << type << std::endl;
    break;
  }
  return event;
}
}

class logger final {
public:
  static spdlog::logger& get_logger(void) {
    static std::shared_ptr<spdlog::logger> logger;
    if (!logger) {
      logger = spdlog::stdout_logger_mt("eventtap", true);
    }
    return *logger;
  }
};

int main(int argc, const char* argv[]) {
  if (getuid() != 0) {
    logger::get_logger().error("eventtap requires root privilege to use kCGHIDEventTap.");
    return 0;
  }

  eventtap_ = CGEventTapCreate(kCGHIDEventTap,
                               kCGHeadInsertEventTap,
                               kCGEventTapOptionDefault,
                               CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventKeyUp),
                               callback,
                               nullptr);

  auto run_loop_source = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventtap_, 0);
  CFRunLoopAddSource(CFRunLoopGetCurrent(), run_loop_source, kCFRunLoopCommonModes);
  CGEventTapEnable(eventtap_, 1);
  CFRelease(run_loop_source);

  CFRunLoopRun();

  return 0;
}
<commit_msg>update eventtap<commit_after>#include <CoreGraphics/CoreGraphics.h>
#include <iostream>
#include <spdlog/spdlog.h>

namespace {
CFMachPortRef eventtap_;

CGEventRef callback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* refcon) {
  std::cout << "CGEventGetFlags 0x" << std::hex << CGEventGetFlags(event) << std::dec << std::endl;

  switch (type) {
  case kCGEventKeyDown:
    std::cout << "kCGEventKeyDown" << std::endl;
    break;
  case kCGEventKeyUp:
    std::cout << "kCGEventKeyUp" << std::endl;
  default:
    std::cout << "callback:" << type << std::endl;
    break;
  }
  return event;
}
}

class logger final {
public:
  static spdlog::logger& get_logger(void) {
    static std::shared_ptr<spdlog::logger> logger;
    if (!logger) {
      logger = spdlog::stdout_logger_mt("eventtap", true);
    }
    return *logger;
  }
};

int main(int argc, const char* argv[]) {
  if (getuid() != 0) {
    logger::get_logger().error("eventtap requires root privilege to use kCGHIDEventTap.");
    return 0;
  }

  eventtap_ = CGEventTapCreate(kCGHIDEventTap,
                               kCGHeadInsertEventTap,
                               kCGEventTapOptionDefault,
                               CGEventMaskBit(kCGEventLeftMouseDown) |
                               CGEventMaskBit(kCGEventLeftMouseUp) |
                               CGEventMaskBit(kCGEventRightMouseDown) |
                               CGEventMaskBit(kCGEventRightMouseUp) |
                               CGEventMaskBit(kCGEventMouseMoved) |
                               CGEventMaskBit(kCGEventLeftMouseDragged) |
                               CGEventMaskBit(kCGEventRightMouseDragged) |
                               CGEventMaskBit(kCGEventKeyDown) |
                               CGEventMaskBit(kCGEventKeyUp),
                               callback,
                               nullptr);

  auto run_loop_source = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventtap_, 0);
  CFRunLoopAddSource(CFRunLoopGetCurrent(), run_loop_source, kCFRunLoopCommonModes);
  CGEventTapEnable(eventtap_, true);
  CFRelease(run_loop_source);

  CFRunLoopRun();

  return 0;
}
<|endoftext|>


<commit_before>
/**
 * THIS IS WORK IN PROGRESS!
 */

#include "Freelist.h"
#include "WTPExceptions.h"
#include "WorkerThreadPool.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <assert.h>
#include <unistd.h>
#include <getopt.h>
#include <map>

using namespace std;
using namespace WTP;
WorkerThreadPool *wtp = NULL;
int globerr = 0;
int rndfd = 0;
Freelist freelist;

class DirProcessor : public WorkItem {
public:
    static void procdir(int dirfd, int level, int nsubdirs, int nfiles, uint64_t filesize);
    static void randfile(int fd, uint64_t size);

    void setParams(int dirfd, int level, int nsubdirs, int nfiles,
                 uint64_t filesize) {
       _dirfd = dirfd;
       _level = level;
       _nsubdirs=nsubdirs;
      _nfiles=nfiles;
      _filesize=filesize;
    }
    void run() {
        procdir(_dirfd, _level, _nsubdirs, _nfiles, _filesize);
    }

    void reset() {
       _dirfd = -1;
       _level = 0;
       _nsubdirs=0;
      _nfiles=0;
      _filesize=0;
    }

private:
    int _dirfd, _level, _nsubdirs, _nfiles;
    uint64_t _filesize;
};

void DirProcessor::randfile(int fd, uint64_t size)
{
    uint64_t remaining = size;
    char *buf = new char[4000];
    while (remaining > 0) {
        int toread = remaining > 4000 ? 4000 : remaining;
        int in = read(rndfd, buf, toread);
        if (in < 0)
            throw string("Failed to read /dev/urandom");
        int wrote = write(fd, buf, in);
        if (wrote < 0)
            throw string("Failed to write to file");
        remaining -= wrote;
    }

    delete[] buf;
}

void DirProcessor::procdir(int dirfd, int level, int nsubdirs, int nfiles, uint64_t filesize)
{
    int fd = 0;
    int i = 0, rc = 0;
    ostringstream ost;

    if (globerr)
        return;
    // Create subdirs
    if (level > 0) {
       for (i=0; i< nsubdirs; i++) {
            ost << "dir" << i;
            const string& dirname = ost.str();
            const char *dirname_c = dirname.c_str();
            rc = mkdirat(dirfd, dirname_c, 0777);
            if (rc < 0) {
                perror("mkdirat");
                throw string("mkdirat() failed");
            }
            fd = openat(dirfd, dirname_c, O_RDONLY);
            if (fd < 0) {
                perror("openat");
                throw string("openat() failed for subdir");
            }
            ost.clear(); ost.str("");
            /*
             * If a DP object is available from freelist, use that.
             * else do recursive processing.
             */
            DirProcessor *dp = dynamic_cast<DirProcessor *>(freelist.getItem());
            if (dp) {
                dp->setParams(fd, level-1, nsubdirs, nfiles, filesize);
                wtp->addWorkItem(dp);
            } else {
                procdir(fd, level-1, nsubdirs, nfiles, filesize);
            }

       }
    }

    for (i=0; i<nfiles; i++) {
        ost << "file" << i;
        const string& filename = ost.str();
        int fd = openat(dirfd, filename.c_str(),
                        O_CREAT | O_WRONLY | O_TRUNC, 0666);
        if (fd < 0)
            throw string("openat() failed");
        randfile(fd, filesize);
        close(fd);
        ost.clear(); ost.str("");
    }

    close(dirfd);

}

int usage_exit(const string& progname, const string& err)
{
    cerr << "Error: " << string(err) << endl;
    cerr << "Usage: " << progname <<
            " --height=height --nfiles=files-perdir --height=tree-height" <<
            " topdir" << endl;

    exit(-1);

}

static std::map<string, int *> valmap;
void setGlobalVal(const string& progname, const string& optname, const string& optvalstr)
{
    std::map<string, int *>::iterator iter = valmap.find(optname);
    assert (iter != valmap.end());
    int *valptr = valmap[optname];
    *valptr = atoi(optvalstr.c_str());
    if (*valptr <= 0)
        usage_exit(progname, string("Option argument must be positive integer"));
}

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

    int ndirs = 4;
    int nfiles = 6;
    int height = 6;
    int filesize = 8000;
    const string progname(argv[0]);

    valmap[string("ndirs")] = &ndirs;
    valmap[string("nfiles")] = &nfiles;
    valmap[string("height")] = &height;
    valmap[string("filesize")] = &filesize;

    int option_index = 0;
    const char *topdir = NULL;
    struct option longopts[] = {
    {"ndirs", required_argument,  0, 0},
    {"nfiles", required_argument,  0, 0},
    {"height", required_argument,  0, 0},
    {"filesize", required_argument,  0, 0},
    {0, 0, 0, 0}
    };

    while (1) {
        int c = getopt_long(argc, argv, "", longopts, &option_index);
        if (c == -1)
            break;
        if (c  != 0)
            usage_exit(progname, string("Command line parse error"));
        string optname(longopts[option_index].name);
        string optvalstr(optarg);

        setGlobalVal(progname, optname, optvalstr);

    }

   if (optind == (argc-1)) {
       topdir = argv[optind];
   } else {
        usage_exit(progname, string("Insufficient arguments"));
   }

try {
    rndfd = open("/dev/urandom", O_RDONLY);
    if (rndfd < 0) {
        perror("/dev/urandom");
        throw string("Failed to open /dev/urandom");
    }
    int rc = mkdir(topdir, 0777);
    if (rc < 0)
        throw string("Failed to create top level dir");
    int dirfd = open(topdir, O_RDONLY);
    if (dirfd < 0)
        throw string("Failed to open top level dir.");

    int i=0;
    for (i=0; i< 1000; i++) {
        DirProcessor *dp = new DirProcessor();
        freelist.addItem(dp);
    }

    wtp = new WorkerThreadPool(16);
    wtp->startThreads();

    DirProcessor *dp = new DirProcessor();
    dp->setParams(dirfd, height, ndirs, nfiles, filesize);

    wtp->addWorkItem(dp);

    cout << "Waiting for empty\n";
    wtp->waitEmpty();
    cout << "WTP empty. Waiting for shutdown.\n";
    wtp->shutDown();
    delete wtp;

    if (globerr < 0)
        cout << "Failure" << endl;

} catch (InternalError& exc) {
    cout << "WTP internal bug or system is in unstable state: " << exc.getMessage() << endl;
} catch (CallerError& exc) {
    cout << "Illegal use of WTP library: " << exc.getMessage() << endl;
} catch(std::string& err) {
    cout << err << endl;
}

}
<commit_msg>Added cpulite. This feature runs treebuild without some cpu-intensive activities, like generating random numbers.<commit_after>
/**
 * THIS IS WORK IN PROGRESS!
 */

#include "Freelist.h"
#include "WTPExceptions.h"
#include "WorkerThreadPool.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <assert.h>
#include <unistd.h>
#include <getopt.h>
#include <map>

using namespace std;
using namespace WTP;
WorkerThreadPool *wtp = NULL;
int globerr = 0;
int rndfd = 0;
Freelist freelist;
int cpulite = 0;
char *litebuf = NULL;

class DirProcessor : public WorkItem {
public:
    static void procdir(int dirfd, int level, int nsubdirs, int nfiles, uint64_t filesize);
    static void randfile(int fd, uint64_t size);
    static void randfile_lite(int fd, uint64_t size);

    void setParams(int dirfd, int level, int nsubdirs, int nfiles,
                 uint64_t filesize) {
       _dirfd = dirfd;
       _level = level;
       _nsubdirs=nsubdirs;
      _nfiles=nfiles;
      _filesize=filesize;
    }
    void run() {
        procdir(_dirfd, _level, _nsubdirs, _nfiles, _filesize);
    }

    void reset() {
       _dirfd = -1;
       _level = 0;
       _nsubdirs=0;
      _nfiles=0;
      _filesize=0;
    }

private:
    int _dirfd, _level, _nsubdirs, _nfiles;
    uint64_t _filesize;
};

void DirProcessor::randfile_lite(int fd, uint64_t size)
{
    uint64_t remaining = size;
    while (remaining > 0) {
        int towrite = remaining > 4000 ? 4000 : remaining;
        int wrote = write(fd, litebuf, towrite);
        if (wrote < 0)
            throw string("Failed to write to file");
        remaining -= wrote;
    }

}
void DirProcessor::randfile(int fd, uint64_t size)
{
    uint64_t remaining = size;
    char *buf = new char[4000];
    while (remaining > 0) {
        int toread = remaining > 4000 ? 4000 : remaining;
        int in = read(rndfd, buf, toread);
        if (in < 0)
            throw string("Failed to read /dev/urandom");
        int wrote = write(fd, buf, in);
        if (wrote < 0)
            throw string("Failed to write to file");
        remaining -= wrote;
    }

    delete[] buf;
}

void DirProcessor::procdir(int dirfd, int level, int nsubdirs, int nfiles, uint64_t filesize)
{
    int fd = 0;
    int i = 0, rc = 0;
    ostringstream ost;

    if (globerr)
        return;
    // Create subdirs
    if (level > 0) {
       for (i=0; i< nsubdirs; i++) {
            ost << "dir" << i;
            const string& dirname = ost.str();
            const char *dirname_c = dirname.c_str();
            rc = mkdirat(dirfd, dirname_c, 0777);
            if (rc < 0) {
                perror("mkdirat");
                throw string("mkdirat() failed");
            }
            fd = openat(dirfd, dirname_c, O_RDONLY);
            if (fd < 0) {
                perror("openat");
                throw string("openat() failed for subdir");
            }
            ost.clear(); ost.str("");
            /*
             * If a DP object is available from freelist, use that.
             * else do recursive processing.
             */
            DirProcessor *dp = dynamic_cast<DirProcessor *>(freelist.getItem());
            if (dp) {
                dp->setParams(fd, level-1, nsubdirs, nfiles, filesize);
                wtp->addWorkItem(dp);
            } else {
                procdir(fd, level-1, nsubdirs, nfiles, filesize);
            }

       }
    }

    for (i=0; i<nfiles; i++) {
        ost << "file" << i;
        const string& filename = ost.str();
        int fd = openat(dirfd, filename.c_str(),
                        O_CREAT | O_WRONLY | O_TRUNC, 0666);
        if (fd < 0)
            throw string("openat() failed");
        if (cpulite)
            randfile_lite(fd, filesize);
        else
            randfile(fd, filesize);
        close(fd);
        ost.clear(); ost.str("");
    }

    close(dirfd);

}

int usage_exit(const string& progname, const string& err)
{
    cerr << "Error: " << string(err) << endl;
    cerr << "Usage: " << progname <<
            " --height=height --nfiles=files-perdir --height=tree-height" <<
            " topdir" << endl;

    exit(-1);

}

static std::map<string, int *> valmap;
void setGlobalVal(const string& progname, const string& optname, const string& optvalstr)
{
    std::map<string, int *>::iterator iter = valmap.find(optname);
    assert (iter != valmap.end());
    int *valptr = valmap[optname];
    *valptr = atoi(optvalstr.c_str());
    if (*valptr <= 0)
        usage_exit(progname, string("Option argument must be positive integer"));
}

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

    int ndirs = 4;
    int nfiles = 6;
    int height = 6;
    int filesize = 8000;
    const string progname(argv[0]);

    valmap[string("ndirs")] = &ndirs;
    valmap[string("nfiles")] = &nfiles;
    valmap[string("height")] = &height;
    valmap[string("filesize")] = &filesize;
    valmap[string("cpulite")] = &cpulite;

    int option_index = 0;
    const char *topdir = NULL;
    struct option longopts[] = {
    {"ndirs", required_argument,  0, 0},
    {"nfiles", required_argument,  0, 0},
    {"height", required_argument,  0, 0},
    {"filesize", required_argument,  0, 0},
    {"cpulite", optional_argument,  0, 0},
    {0, 0, 0, 0}
    };

    while (1) {
        int c = getopt_long(argc, argv, "", longopts, &option_index);
        if (c == -1)
            break;
        if (c  != 0)
            usage_exit(progname, string("Command line parse error"));
        string optname(longopts[option_index].name);
        string optvalstr(optarg);

        setGlobalVal(progname, optname, optvalstr);

    }

   if (optind == (argc-1)) {
       topdir = argv[optind];
   } else {
        usage_exit(progname, string("Insufficient arguments"));
   }

try {
    rndfd = open("/dev/urandom", O_RDONLY);
    if (rndfd < 0) {
        perror("/dev/urandom");
        throw string("Failed to open /dev/urandom");
    }
    if (cpulite) {
	    litebuf = new char[4000];
        read(rndfd, litebuf, 4000);
        close(rndfd);
	}
    int rc = mkdir(topdir, 0777);
    if (rc < 0)
        throw string("Failed to create top level dir");
    int dirfd = open(topdir, O_RDONLY);
    if (dirfd < 0)
        throw string("Failed to open top level dir.");

    int i=0;
    for (i=0; i< 1000; i++) {
        DirProcessor *dp = new DirProcessor();
        freelist.addItem(dp);
    }

    wtp = new WorkerThreadPool(16);
    wtp->startThreads();

    DirProcessor *dp = new DirProcessor();
    dp->setParams(dirfd, height, ndirs, nfiles, filesize);

    wtp->addWorkItem(dp);

    cout << "Waiting for empty\n";
    wtp->waitEmpty();
    cout << "WTP empty. Waiting for shutdown.\n";
    wtp->shutDown();
    delete wtp;
    if (cpulite)
        delete[] litebuf;

    if (globerr < 0)
        cout << "Failure" << endl;

} catch (InternalError& exc) {
    cout << "WTP internal bug or system is in unstable state: " << exc.getMessage() << endl;
} catch (CallerError& exc) {
    cout << "Illegal use of WTP library: " << exc.getMessage() << endl;
} catch(std::string& err) {
    cout << err << endl;
}

}
<|endoftext|>


<commit_before>#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ds3.h"
#include "test.h"
#include <boost/test/unit_test.hpp>

#define FILE_TEMPLATE "bulk-XXXXXX"

BOOST_AUTO_TEST_CASE( bulk_get ) {
    uint64_t i, n;
    uint64_t file_index = 0;
    ds3_request* request = NULL;
    ds3_error* error = NULL;
    ds3_bulk_response* completed_job = NULL;
    ds3_get_bucket_response* response = NULL;
    ds3_bulk_response* bulk_response = NULL;
    ds3_bulk_object_list* object_list = NULL;
    ds3_get_available_chunks_response* chunk_response = NULL;
    bool retry_get;

    ds3_client* client = get_client();
    const char* bucket_name = "unit_test_bucket";

    char** tmp_files;

    populate_with_objects(client, bucket_name);

    request = ds3_init_get_bucket(bucket_name);
    error = ds3_get_bucket(client, request, &response);

    ds3_free_request(request);

    BOOST_REQUIRE(error == NULL);

    tmp_files = (char**) calloc(response->num_objects, sizeof(char*));

    object_list = ds3_convert_object_list(response->objects, response->num_objects);

    request = ds3_init_get_bulk(bucket_name, object_list, NONE);
    error = ds3_bulk(client, request, &bulk_response);

    ds3_free_request(request);
    ds3_free_bulk_object_list(object_list);

    BOOST_REQUIRE(error == NULL);

    do {
        retry_get = false;
        request = ds3_init_get_available_chunks(bulk_response->job_id->value);

        error = ds3_get_available_chunks(client, request, &chunk_response);

        ds3_free_request(request);

        BOOST_REQUIRE(error == NULL);

        BOOST_REQUIRE(chunk_response != NULL);

        if (chunk_response->object_list->list_size == 0) {
            // if this happens we need to try the request
            retry_get = true;
            BOOST_TEST_MESSAGE( "Hit retry, sleeping for: " << chunk_response->retry_after) ;
            sleep(chunk_response->retry_after);
        }

    } while(retry_get);

    BOOST_REQUIRE(error == NULL);

    for (i = 0; i < chunk_response->object_list->list_size; i++) {
        ds3_bulk_object_list* chunk_object_list = chunk_response->object_list->list[i];
        for(n = 0; n < chunk_object_list->size; n++, file_index++) {
            FILE* w_file;
            ds3_bulk_object current_obj = chunk_object_list->list[n];
            request = ds3_init_get_object_for_job(bucket_name, current_obj.name->value, current_obj.offset, bulk_response->job_id->value);
            tmp_files[file_index] = (char*) calloc(13, sizeof(char));
            memcpy(tmp_files[file_index], FILE_TEMPLATE, 12);
            w_file = fopen(tmp_files[file_index], "w+");
            error = ds3_get_object(client, request, w_file, ds3_write_to_file);
            fclose(w_file);
            handle_error(error);
        }
    }

    for (i = 0; i <= file_index;i++) {
        unlink(tmp_files[i]);
    }

    free(tmp_files);

    // check to make sure that the 'job' has completed
    request = ds3_init_get_job(bulk_response->job_id->value);
    error = ds3_get_job(client, request, &completed_job);

    BOOST_CHECK(completed_job != NULL);
    BOOST_CHECK(completed_job->status == COMPLETED);

    ds3_free_request(request);
    ds3_free_available_chunks_response(chunk_response);
    ds3_free_bulk_response(completed_job);
    ds3_free_bulk_response(bulk_response);

    clear_bucket(client, bucket_name);

    handle_error(error);
}

BOOST_AUTO_TEST_CASE( convert_list_helper ) {
    const char* books[4] ={"beowulf.txt", "sherlock_holmes.txt", "tale_of_two_cities.txt", "ulysses.txt"};
    ds3_bulk_object_list* obj_list;

    obj_list = ds3_convert_file_list_with_basepath(books, 4, "resources/");

    BOOST_CHECK(strcmp(obj_list->list[0].name->value, "beowulf.txt") == 0);
    BOOST_CHECK(obj_list->list[0].length == 294059);
}

BOOST_AUTO_TEST_CASE( directory_size ) {
    const char* books[1] ={"resources"};
    ds3_bulk_object_list* obj_list;

    obj_list = ds3_convert_file_list_with_basepath(books, 1, NULL);

    BOOST_CHECK(strcmp(obj_list->list[0].name->value, "resources") == 0);
    BOOST_CHECK(obj_list->list[0].length == 0);
}
<commit_msg>Added a missing free in the bulk_get integration test.<commit_after>#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ds3.h"
#include "test.h"
#include <boost/test/unit_test.hpp>

#define FILE_TEMPLATE "bulk-XXXXXX"

BOOST_AUTO_TEST_CASE( bulk_get ) {
    uint64_t i, n;
    uint64_t file_index = 0;
    ds3_request* request = NULL;
    ds3_error* error = NULL;
    ds3_bulk_response* completed_job = NULL;
    ds3_get_bucket_response* response = NULL;
    ds3_bulk_response* bulk_response = NULL;
    ds3_bulk_object_list* object_list = NULL;
    ds3_get_available_chunks_response* chunk_response = NULL;
    bool retry_get;

    ds3_client* client = get_client();
    const char* bucket_name = "unit_test_bucket";

    char** tmp_files;

    populate_with_objects(client, bucket_name);

    request = ds3_init_get_bucket(bucket_name);
    error = ds3_get_bucket(client, request, &response);

    ds3_free_request(request);

    BOOST_REQUIRE(error == NULL);

    tmp_files = (char**) calloc(response->num_objects, sizeof(char*));

    object_list = ds3_convert_object_list(response->objects, response->num_objects);

    request = ds3_init_get_bulk(bucket_name, object_list, NONE);
    error = ds3_bulk(client, request, &bulk_response);

    ds3_free_request(request);
    ds3_free_bulk_object_list(object_list);

    BOOST_REQUIRE(error == NULL);

    do {
        retry_get = false;
        request = ds3_init_get_available_chunks(bulk_response->job_id->value);

        error = ds3_get_available_chunks(client, request, &chunk_response);

        ds3_free_request(request);

        BOOST_REQUIRE(error == NULL);

        BOOST_REQUIRE(chunk_response != NULL);

        if (chunk_response->object_list->list_size == 0) {
            // if this happens we need to try the request
            retry_get = true;
            BOOST_TEST_MESSAGE( "Hit retry, sleeping for: " << chunk_response->retry_after) ;
            sleep(chunk_response->retry_after);
        }

    } while(retry_get);

    BOOST_REQUIRE(error == NULL);

    for (i = 0; i < chunk_response->object_list->list_size; i++) {
        ds3_bulk_object_list* chunk_object_list = chunk_response->object_list->list[i];
        for(n = 0; n < chunk_object_list->size; n++, file_index++) {
            FILE* w_file;
            ds3_bulk_object current_obj = chunk_object_list->list[n];
            request = ds3_init_get_object_for_job(bucket_name, current_obj.name->value, current_obj.offset, bulk_response->job_id->value);
            tmp_files[file_index] = (char*) calloc(13, sizeof(char));
            memcpy(tmp_files[file_index], FILE_TEMPLATE, 12);
            w_file = fopen(tmp_files[file_index], "w+");
            error = ds3_get_object(client, request, w_file, ds3_write_to_file);
            ds3_free_request(request);
            fclose(w_file);
            handle_error(error);
        }
    }

    for (i = 0; i <= file_index;i++) {
        unlink(tmp_files[i]);
    }

    free(tmp_files);

    // check to make sure that the 'job' has completed
    request = ds3_init_get_job(bulk_response->job_id->value);
    error = ds3_get_job(client, request, &completed_job);

    BOOST_CHECK(completed_job != NULL);
    BOOST_CHECK(completed_job->status == COMPLETED);

    ds3_free_request(request);
    ds3_free_available_chunks_response(chunk_response);
    ds3_free_bulk_response(completed_job);
    ds3_free_bulk_response(bulk_response);

    clear_bucket(client, bucket_name);

    handle_error(error);
}

BOOST_AUTO_TEST_CASE( convert_list_helper ) {
    const char* books[4] ={"beowulf.txt", "sherlock_holmes.txt", "tale_of_two_cities.txt", "ulysses.txt"};
    ds3_bulk_object_list* obj_list;

    obj_list = ds3_convert_file_list_with_basepath(books, 4, "resources/");

    BOOST_CHECK(strcmp(obj_list->list[0].name->value, "beowulf.txt") == 0);
    BOOST_CHECK(obj_list->list[0].length == 294059);
}

BOOST_AUTO_TEST_CASE( directory_size ) {
    const char* books[1] ={"resources"};
    ds3_bulk_object_list* obj_list;

    obj_list = ds3_convert_file_list_with_basepath(books, 1, NULL);

    BOOST_CHECK(strcmp(obj_list->list[0].name->value, "resources") == 0);
    BOOST_CHECK(obj_list->list[0].length == 0);
}
<|endoftext|>


<commit_before>// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "main.h"

#if EIGEN_ALIGN
#define ALIGNMENT 16
#else
#define ALIGNMENT 1
#endif

void check_handmade_aligned_malloc()
{
  for(int i = 1; i < 1000; i++)
  {
    char *p = (char*)internal::handmade_aligned_malloc(i);
    VERIFY(size_t(p)%ALIGNMENT==0);
    // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
    for(int j = 0; j < i; j++) p[j]=0;
    internal::handmade_aligned_free(p);
  }
}

void check_aligned_malloc()
{
  for(int i = 1; i < 1000; i++)
  {
    char *p = (char*)internal::aligned_malloc(i);
    VERIFY(size_t(p)%ALIGNMENT==0);
    // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
    for(int j = 0; j < i; j++) p[j]=0;
    internal::aligned_free(p);
  }
}

void check_aligned_new()
{
  for(int i = 1; i < 1000; i++)
  {
    float *p = internal::aligned_new<float>(i);
    VERIFY(size_t(p)%ALIGNMENT==0);
    // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
    for(int j = 0; j < i; j++) p[j]=0;
    internal::aligned_delete(p,i);
  }
}

void check_aligned_stack_alloc()
{
  for(int i = 1; i < 1000; i++)
  {
    ei_declare_aligned_stack_constructed_variable(float,p,i,0);
    VERIFY(size_t(p)%ALIGNMENT==0);
    // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
    for(int j = 0; j < i; j++) p[j]=0;
  }
}

// test compilation with both a struct and a class...
struct MyStruct
{
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  char dummychar;
  Vector4f avec;
};

class MyClassA
{
  public:
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
    char dummychar;
    Vector4f avec;
};

template<typename T> void check_dynaligned()
{
  T* obj = new T;
  VERIFY(size_t(obj)%ALIGNMENT==0);
  delete obj;
}

void test_dynalloc()
{
  // low level dynamic memory allocation
  CALL_SUBTEST(check_handmade_aligned_malloc());
  CALL_SUBTEST(check_aligned_malloc());
  CALL_SUBTEST(check_aligned_new());
  CALL_SUBTEST(check_aligned_stack_alloc());

  for (int i=0; i<g_repeat*100; ++i)
  {
    CALL_SUBTEST(check_dynaligned<Vector4f>() );
    CALL_SUBTEST(check_dynaligned<Vector2d>() );
    CALL_SUBTEST(check_dynaligned<Matrix4f>() );
    CALL_SUBTEST(check_dynaligned<Vector4d>() );
    CALL_SUBTEST(check_dynaligned<Vector4i>() );
  }

  // check static allocation, who knows ?
  #if EIGEN_ALIGN_STATICALLY
  {
    MyStruct foo0;  VERIFY(size_t(foo0.avec.data())%ALIGNMENT==0);
    MyClassA fooA;  VERIFY(size_t(fooA.avec.data())%ALIGNMENT==0);
  }

  // dynamic allocation, single object
  for (int i=0; i<g_repeat*100; ++i)
  {
    MyStruct *foo0 = new MyStruct();  VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0);
    MyClassA *fooA = new MyClassA();  VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0);
    delete foo0;
    delete fooA;
  }

  // dynamic allocation, array
  const int N = 10;
  for (int i=0; i<g_repeat*100; ++i)
  {
    MyStruct *foo0 = new MyStruct[N];  VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0);
    MyClassA *fooA = new MyClassA[N];  VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0);
    delete[] foo0;
    delete[] fooA;
  }
  #endif

}
<commit_msg>Check that NeedsToAlign is properly sets before checking alignment<commit_after>// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "main.h"

#if EIGEN_ALIGN
#define ALIGNMENT 16
#else
#define ALIGNMENT 1
#endif

void check_handmade_aligned_malloc()
{
  for(int i = 1; i < 1000; i++)
  {
    char *p = (char*)internal::handmade_aligned_malloc(i);
    VERIFY(size_t(p)%ALIGNMENT==0);
    // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
    for(int j = 0; j < i; j++) p[j]=0;
    internal::handmade_aligned_free(p);
  }
}

void check_aligned_malloc()
{
  for(int i = 1; i < 1000; i++)
  {
    char *p = (char*)internal::aligned_malloc(i);
    VERIFY(size_t(p)%ALIGNMENT==0);
    // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
    for(int j = 0; j < i; j++) p[j]=0;
    internal::aligned_free(p);
  }
}

void check_aligned_new()
{
  for(int i = 1; i < 1000; i++)
  {
    float *p = internal::aligned_new<float>(i);
    VERIFY(size_t(p)%ALIGNMENT==0);
    // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
    for(int j = 0; j < i; j++) p[j]=0;
    internal::aligned_delete(p,i);
  }
}

void check_aligned_stack_alloc()
{
  for(int i = 1; i < 1000; i++)
  {
    ei_declare_aligned_stack_constructed_variable(float,p,i,0);
    VERIFY(size_t(p)%ALIGNMENT==0);
    // if the buffer is wrongly allocated this will give a bad write --> check with valgrind
    for(int j = 0; j < i; j++) p[j]=0;
  }
}

// test compilation with both a struct and a class...
struct MyStruct
{
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  char dummychar;
  Vector4f avec;
};

class MyClassA
{
  public:
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
    char dummychar;
    Vector4f avec;
};

template<typename T> void check_dynaligned()
{
  T* obj = new T;
  VERIFY(T::NeedsToAlign==1);
  VERIFY(size_t(obj)%ALIGNMENT==0);
  delete obj;
}

void test_dynalloc()
{
  // low level dynamic memory allocation
  CALL_SUBTEST(check_handmade_aligned_malloc());
  CALL_SUBTEST(check_aligned_malloc());
  CALL_SUBTEST(check_aligned_new());
  CALL_SUBTEST(check_aligned_stack_alloc());

  for (int i=0; i<g_repeat*100; ++i)
  {
    CALL_SUBTEST(check_dynaligned<Vector4f>() );
    CALL_SUBTEST(check_dynaligned<Vector2d>() );
    CALL_SUBTEST(check_dynaligned<Matrix4f>() );
    CALL_SUBTEST(check_dynaligned<Vector4d>() );
    CALL_SUBTEST(check_dynaligned<Vector4i>() );
  }

  // check static allocation, who knows ?
  #if EIGEN_ALIGN_STATICALLY
  {
    MyStruct foo0;  VERIFY(size_t(foo0.avec.data())%ALIGNMENT==0);
    MyClassA fooA;  VERIFY(size_t(fooA.avec.data())%ALIGNMENT==0);
  }

  // dynamic allocation, single object
  for (int i=0; i<g_repeat*100; ++i)
  {
    MyStruct *foo0 = new MyStruct();  VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0);
    MyClassA *fooA = new MyClassA();  VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0);
    delete foo0;
    delete fooA;
  }

  // dynamic allocation, array
  const int N = 10;
  for (int i=0; i<g_repeat*100; ++i)
  {
    MyStruct *foo0 = new MyStruct[N];  VERIFY(size_t(foo0->avec.data())%ALIGNMENT==0);
    MyClassA *fooA = new MyClassA[N];  VERIFY(size_t(fooA->avec.data())%ALIGNMENT==0);
    delete[] foo0;
    delete[] fooA;
  }
  #endif

}
<|endoftext|>


<commit_before>/*
 * Copyright (c) 2001 Stephen Williams (steve@icarus.com)
 *
 *    This source code is free software; you can redistribute it
 *    and/or modify it in source code form under the terms of the GNU
 *    General Public License as published by the Free Software
 *    Foundation; either version 2 of the License, or (at your option)
 *    any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */
#if !defined(WINNT)
#ident "$Id: load_module.cc,v 1.3 2001/11/16 05:07:19 steve Exp $"
#endif

# include  "config.h"
# include  "util.h"
# include  "parse_api.h"
# include  "compiler.h"
# include  <iostream.h>

const char dir_character = '/';

bool load_module(const char*type)
{
      char path[4096];

      for (list<const char*>::iterator cur = library_dirs.begin()
		 ; cur != library_dirs.end()
		 ; cur ++ ) {
	    for (list<const char*>::iterator suf = library_suff.begin()
		       ; suf != library_suff.end()
		       ; suf ++ ) {

		  sprintf(path, "%s%c%s%s", *cur, dir_character, type, *suf);

		  FILE*file = fopen(path, "r");
		  if (file == 0)
			continue;

		  if (verbose_flag) {
			cerr << "Loading library file " << path << "." << endl;
		  }

		  pform_parse(path, file);
		  return true;
	    }
      }

      return false;
}

/*
 * $Log: load_module.cc,v $
 * Revision 1.3  2001/11/16 05:07:19  steve
 *  Add support for +libext+ in command files.
 *
 * Revision 1.2  2001/10/22 02:05:21  steve
 *  Handle activating tasks in another root.
 *
 * Revision 1.1  2001/10/20 23:02:40  steve
 *  Add automatic module libraries.
 *
 */

<commit_msg> Close library files after parsing.<commit_after>/*
 * Copyright (c) 2001 Stephen Williams (steve@icarus.com)
 *
 *    This source code is free software; you can redistribute it
 *    and/or modify it in source code form under the terms of the GNU
 *    General Public License as published by the Free Software
 *    Foundation; either version 2 of the License, or (at your option)
 *    any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */
#if !defined(WINNT)
#ident "$Id: load_module.cc,v 1.4 2001/11/20 23:36:34 steve Exp $"
#endif

# include  "config.h"
# include  "util.h"
# include  "parse_api.h"
# include  "compiler.h"
# include  <iostream.h>

const char dir_character = '/';

bool load_module(const char*type)
{
      char path[4096];

      for (list<const char*>::iterator cur = library_dirs.begin()
		 ; cur != library_dirs.end()
		 ; cur ++ ) {
	    for (list<const char*>::iterator suf = library_suff.begin()
		       ; suf != library_suff.end()
		       ; suf ++ ) {

		  sprintf(path, "%s%c%s%s", *cur, dir_character, type, *suf);

		  FILE*file = fopen(path, "r");
		  if (file == 0)
			continue;

		  if (verbose_flag) {
			cerr << "Loading library file " << path << "." << endl;
		  }

		  pform_parse(path, file);

		  fclose(file);
		  return true;
	    }
      }

      return false;
}

/*
 * $Log: load_module.cc,v $
 * Revision 1.4  2001/11/20 23:36:34  steve
 *  Close library files after parsing.
 *
 * Revision 1.3  2001/11/16 05:07:19  steve
 *  Add support for +libext+ in command files.
 *
 * Revision 1.2  2001/10/22 02:05:21  steve
 *  Handle activating tasks in another root.
 *
 * Revision 1.1  2001/10/20 23:02:40  steve
 *  Add automatic module libraries.
 *
 */

<|endoftext|>


<commit_before>// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef __STOUT_OS_WINDOWS_FD_HPP__
#define __STOUT_OS_WINDOWS_FD_HPP__

#include <fcntl.h> // For `O_RDWR`.
#include <io.h> // For `_open_osfhandle`.

#include <array>
#include <memory>
#include <ostream>
#include <type_traits>

#include <stout/check.hpp>
#include <stout/nothing.hpp>
#include <stout/try.hpp>
#include <stout/unreachable.hpp>
#include <stout/windows.hpp> // For `WinSock2.h`.

namespace os {

// The `WindowsFD` class exists to provide an common interface with the POSIX
// file descriptor. While the bare `int` representation of the POSIX file
// descriptor API is undesirable, we rendezvous there in order to maintain the
// existing code in Mesos.
//
// In the platform-agnostic code paths, the `int_fd` type is aliased to
// `WindowsFD`. The `os::*` functions return a type appropriate to the platform,
// which allows us to write code like this:
//
//   Try<int_fd> fd = os::open(...);
//
// The `WindowsFD` constructs off one of:
//   (1) `HANDLE` - from the Win32 API
//   (2) `SOCKET` - from the WinSock API
//
// The `os::*` functions then take an instance of `WindowsFD`, examines
// the state and dispatches to the appropriate API.

class WindowsFD
{
public:
  enum class Type
  {
    HANDLE,
    SOCKET
  };

  // The `HANDLE` here is expected to be file handles. Specifically,
  // `HANDLE`s returned by file API such as `CreateFile`. There are
  // APIs that return `HANDLE`s with different error values, and
  // therefore must be handled accordingly. For example, a thread API
  // such as `CreateThread` returns `NULL` as the error value, rather
  // than `INVALID_HANDLE_VALUE`.
  //
  // TODO(mpark): Consider adding a second parameter which tells us
  //              what the error values are.
  static_assert(
      std::is_same<HANDLE, void*>::value,
      "Expected `HANDLE` to be of type `void*`.");
  explicit WindowsFD(HANDLE handle, bool overlapped = false)
    : type_(Type::HANDLE), handle_(handle), overlapped_(overlapped)
  {}

  // The `SOCKET` here is expected to be Windows sockets, such as that
  // used by the Windows Sockets 2 library. The only expected error
  // value is `INVALID_SOCKET`.
  //
  // Note that sockets should almost always be overlapped. We do provide
  // a way in stout to create non-overlapped sockets, so for completeness, we
  // have an overlapped parameter in the constructor.
  static_assert(
      std::is_same<SOCKET, unsigned __int64>::value,
      "Expected `SOCKET` to be of type `unsigned __int64`.");
  explicit WindowsFD(SOCKET socket, bool overlapped = true)
    : type_(Type::SOCKET), socket_(socket), overlapped_(overlapped)
  {}

  // On Windows, libevent's `evutil_socket_t` is set to `intptr_t`.
  explicit WindowsFD(intptr_t socket) : WindowsFD(static_cast<SOCKET>(socket))
  {}

  // This constructor is provided in so that the canonical integer
  // file descriptors representing `stdin` (0), `stdout` (1), and
  // `stderr` (2), and the invalid value of -1 can be used.
  WindowsFD(int crt) : WindowsFD(INVALID_HANDLE_VALUE)
  {
    if (crt == -1) {
      // No-op, already `INVALID_HANDLE_VALUE`.
    } else if (crt == 0) {
      handle_ = ::GetStdHandle(STD_INPUT_HANDLE);
    } else if (crt == 1) {
      handle_ = ::GetStdHandle(STD_OUTPUT_HANDLE);
    } else if (crt == 2) {
      handle_ = ::GetStdHandle(STD_ERROR_HANDLE);
    } else {
      // This would be better enforced at compile-time, but not
      // currently possible, so this is a sanity check.
      LOG(FATAL) << "Unexpected construction of `WindowsFD`";
    }
  }

  // Default construct with invalid handle semantics.
  WindowsFD() : WindowsFD(INVALID_HANDLE_VALUE) {}

  WindowsFD(const WindowsFD&) = default;
  WindowsFD(WindowsFD&&) = default;

  WindowsFD& operator=(const WindowsFD&) = default;
  WindowsFD& operator=(WindowsFD&&) = default;

  ~WindowsFD() = default;

  bool is_valid() const
  {
    switch (type()) {
      case Type::HANDLE: {
        // Remember that both of these values can represent an invalid
        // handle.
        return handle_ != nullptr && handle_ != INVALID_HANDLE_VALUE;
      }
      case Type::SOCKET: {
        // Only this value is used for an invalid socket.
        return socket_ != INVALID_SOCKET;
      }
      default: {
        return false;
      }
    }
  }

  // NOTE: This allocates a C run-time file descriptor and associates
  // it with the handle. At this point, the `HANDLE` should no longer
  // be closed via `CloseHandle`, but instead close the returned `int`
  // with `_close`. This method should almost never be used, and
  // exists only for compatibility with 3rdparty dependencies.
  int crt() const
  {
    CHECK_EQ(Type::HANDLE, type());
    // TODO(andschwa): Consider if we should overwrite `handle_`.
    return ::_open_osfhandle(reinterpret_cast<intptr_t>(handle_), O_RDWR);
  }

  operator HANDLE() const
  {
    CHECK_EQ(Type::HANDLE, type());
    return handle_;
  }

  operator SOCKET() const
  {
    CHECK_EQ(Type::SOCKET, type());
    return socket_;
  }

  // On Windows, libevent's `evutil_socket_t` is set to `intptr_t`.
  operator intptr_t() const
  {
    CHECK_EQ(Type::SOCKET, type());
    return static_cast<intptr_t>(socket_);
  }

  Type type() const { return type_; }

  bool is_overlapped() const { return overlapped_; }

private:
  Type type_;

  union
  {
    HANDLE handle_;
    SOCKET socket_;
  };

  bool overlapped_;

  // NOTE: This function is provided only for checking validity, thus
  // it is private. It provides a view of a `WindowsFD` as an `int`.
  //
  // TODO(andschwa): Fix all uses of this conversion to use `is_valid`
  // directly instead, then remove the comparison operators. This
  // would require writing an `int_fd` class for POSIX too, instead of
  // just using `int`.
  int get_valid() const
  {
    if (is_valid()) {
      return 0;
    } else {
      return -1;
    }
  }

  // NOTE: These operators are used solely to support checking a
  // `WindowsFD` against e.g. -1 or 0 for validity. Nothing else
  // should have access to `get_valid()`.
  friend bool operator<(int left, const WindowsFD& right);
  friend bool operator<(const WindowsFD& left, int right);
  friend bool operator>(int left, const WindowsFD& right);
  friend bool operator>(const WindowsFD& left, int right);
  friend bool operator<=(int left, const WindowsFD& right);
  friend bool operator<=(const WindowsFD& left, int right);
  friend bool operator>=(int left, const WindowsFD& right);
  friend bool operator>=(const WindowsFD& left, int right);
  friend bool operator==(int left, const WindowsFD& right);
  friend bool operator==(const WindowsFD& left, int right);
  friend bool operator!=(int left, const WindowsFD& right);
  friend bool operator!=(const WindowsFD& left, int right);
};

inline std::ostream& operator<<(std::ostream& stream, const WindowsFD::Type& fd)
{
  switch (fd) {
    case WindowsFD::Type::HANDLE: {
      stream << "WindowsFD::Type::HANDLE";
      return stream;
    }
    case WindowsFD::Type::SOCKET: {
      stream << "WindowsFD::Type::SOCKET";
      return stream;
    }
    default: {
      stream << "WindowsFD::Type::UNKNOWN";
      return stream;
    }
  }
}

inline std::ostream& operator<<(std::ostream& stream, const WindowsFD& fd)
{
  stream << fd.type() << "=";
  switch (fd.type()) {
    case WindowsFD::Type::HANDLE: {
      stream << static_cast<HANDLE>(fd);
      return stream;
    }
    case WindowsFD::Type::SOCKET: {
      stream << static_cast<SOCKET>(fd);
      return stream;
    }
    default: {
      stream << "UNKNOWN";
      return stream;
    }
  }
}

// NOTE: The following operators implement all the comparisons
// possible a `WindowsFD` type and an `int`. The point of this is that
// the `WindowsFD` type must act like an `int` for compatibility
// reasons (e.g. checking validity through `fd < 0`), without actually
// being castable to an `int` to avoid ambiguous types.
inline bool operator<(int left, const WindowsFD& right)
{
  return left < right.get_valid();
}

inline bool operator<(const WindowsFD& left, int right)
{
  return left.get_valid() < right;
}

inline bool operator>(int left, const WindowsFD& right)
{
  return left > right.get_valid();
}

inline bool operator>(const WindowsFD& left, int right)
{
  return left.get_valid() > right;
}

inline bool operator<=(int left, const WindowsFD& right)
{
  return left <= right.get_valid();
}

inline bool operator<=(const WindowsFD& left, int right)
{
  return left.get_valid() <= right;
}

inline bool operator>=(int left, const WindowsFD& right)
{
  return left >= right.get_valid();
}

inline bool operator>=(const WindowsFD& left, int right)
{
  return left.get_valid() >= right;
}

inline bool operator==(int left, const WindowsFD& right)
{
  return left == right.get_valid();
}

inline bool operator==(const WindowsFD& left, int right)
{
  return left.get_valid() == right;
}

inline bool operator!=(int left, const WindowsFD& right)
{
  return left != right.get_valid();
}

inline bool operator!=(const WindowsFD& left, int right)
{
  return left.get_valid() != right;
}

// NOTE: This operator exists so that `WindowsFD` can be used in an
// `unordered_map` (and other STL containers requiring equality).
inline bool operator==(const WindowsFD& left, const WindowsFD& right)
{
  // This is `true` even if the types mismatch because we want
  // `WindowsFD(-1)` to compare as equivalent to an invalid `HANDLE`
  // or `SOCKET`, even though it is technically of type `HANDLE`.
  if (!left.is_valid() && !right.is_valid()) {
    return true;
  }

  // Otherwise mismatched types are not equivalent.
  if (left.type() != right.type()) {
    return false;
  }

  switch (left.type()) {
    case WindowsFD::Type::HANDLE: {
      return static_cast<HANDLE>(left) == static_cast<HANDLE>(right);
    }
    case WindowsFD::Type::SOCKET: {
      return static_cast<SOCKET>(left) == static_cast<SOCKET>(right);
    }
  }

  UNREACHABLE();
}

} // namespace os {

namespace std {

// NOTE: This specialization exists so that `WindowsFD` can be used in
// an `unordered_map` (and other STL containers requiring a hash).
template <>
struct hash<os::WindowsFD>
{
  using argument_type = os::WindowsFD;
  using result_type = size_t;

  result_type operator()(const argument_type& fd) const noexcept
  {
    switch (fd.type()) {
      case os::WindowsFD::Type::HANDLE: {
        return std::hash<HANDLE>{}(static_cast<HANDLE>(fd));
      }
      case os::WindowsFD::Type::SOCKET: {
        return std::hash<SOCKET>{}(static_cast<SOCKET>(fd));
      }
    }

    UNREACHABLE();
  }
};

} // namespace std {

#endif // __STOUT_OS_WINDOWS_FD_HPP__
<commit_msg>Windows: Made socket `int_fd` castable to `HANDLE` type.<commit_after>// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef __STOUT_OS_WINDOWS_FD_HPP__
#define __STOUT_OS_WINDOWS_FD_HPP__

#include <fcntl.h> // For `O_RDWR`.
#include <io.h> // For `_open_osfhandle`.

#include <array>
#include <memory>
#include <ostream>
#include <type_traits>

#include <stout/check.hpp>
#include <stout/nothing.hpp>
#include <stout/try.hpp>
#include <stout/unreachable.hpp>
#include <stout/windows.hpp> // For `WinSock2.h`.

namespace os {

// The `WindowsFD` class exists to provide an common interface with the POSIX
// file descriptor. While the bare `int` representation of the POSIX file
// descriptor API is undesirable, we rendezvous there in order to maintain the
// existing code in Mesos.
//
// In the platform-agnostic code paths, the `int_fd` type is aliased to
// `WindowsFD`. The `os::*` functions return a type appropriate to the platform,
// which allows us to write code like this:
//
//   Try<int_fd> fd = os::open(...);
//
// The `WindowsFD` constructs off one of:
//   (1) `HANDLE` - from the Win32 API
//   (2) `SOCKET` - from the WinSock API
//
// The `os::*` functions then take an instance of `WindowsFD`, examines
// the state and dispatches to the appropriate API.

class WindowsFD
{
public:
  enum class Type
  {
    HANDLE,
    SOCKET
  };

  // The `HANDLE` here is expected to be file handles. Specifically,
  // `HANDLE`s returned by file API such as `CreateFile`. There are
  // APIs that return `HANDLE`s with different error values, and
  // therefore must be handled accordingly. For example, a thread API
  // such as `CreateThread` returns `NULL` as the error value, rather
  // than `INVALID_HANDLE_VALUE`.
  //
  // TODO(mpark): Consider adding a second parameter which tells us
  //              what the error values are.
  static_assert(
      std::is_same<HANDLE, void*>::value,
      "Expected `HANDLE` to be of type `void*`.");
  explicit WindowsFD(HANDLE handle, bool overlapped = false)
    : type_(Type::HANDLE), handle_(handle), overlapped_(overlapped)
  {}

  // The `SOCKET` here is expected to be Windows sockets, such as that
  // used by the Windows Sockets 2 library. The only expected error
  // value is `INVALID_SOCKET`.
  //
  // Note that sockets should almost always be overlapped. We do provide
  // a way in stout to create non-overlapped sockets, so for completeness, we
  // have an overlapped parameter in the constructor.
  static_assert(
      std::is_same<SOCKET, unsigned __int64>::value,
      "Expected `SOCKET` to be of type `unsigned __int64`.");
  explicit WindowsFD(SOCKET socket, bool overlapped = true)
    : type_(Type::SOCKET), socket_(socket), overlapped_(overlapped)
  {}

  // On Windows, libevent's `evutil_socket_t` is set to `intptr_t`.
  explicit WindowsFD(intptr_t socket) : WindowsFD(static_cast<SOCKET>(socket))
  {}

  // This constructor is provided in so that the canonical integer
  // file descriptors representing `stdin` (0), `stdout` (1), and
  // `stderr` (2), and the invalid value of -1 can be used.
  WindowsFD(int crt) : WindowsFD(INVALID_HANDLE_VALUE)
  {
    if (crt == -1) {
      // No-op, already `INVALID_HANDLE_VALUE`.
    } else if (crt == 0) {
      handle_ = ::GetStdHandle(STD_INPUT_HANDLE);
    } else if (crt == 1) {
      handle_ = ::GetStdHandle(STD_OUTPUT_HANDLE);
    } else if (crt == 2) {
      handle_ = ::GetStdHandle(STD_ERROR_HANDLE);
    } else {
      // This would be better enforced at compile-time, but not
      // currently possible, so this is a sanity check.
      LOG(FATAL) << "Unexpected construction of `WindowsFD`";
    }
  }

  // Default construct with invalid handle semantics.
  WindowsFD() : WindowsFD(INVALID_HANDLE_VALUE) {}

  WindowsFD(const WindowsFD&) = default;
  WindowsFD(WindowsFD&&) = default;

  WindowsFD& operator=(const WindowsFD&) = default;
  WindowsFD& operator=(WindowsFD&&) = default;

  ~WindowsFD() = default;

  bool is_valid() const
  {
    switch (type()) {
      case Type::HANDLE: {
        // Remember that both of these values can represent an invalid
        // handle.
        return handle_ != nullptr && handle_ != INVALID_HANDLE_VALUE;
      }
      case Type::SOCKET: {
        // Only this value is used for an invalid socket.
        return socket_ != INVALID_SOCKET;
      }
      default: {
        return false;
      }
    }
  }

  // NOTE: This allocates a C run-time file descriptor and associates
  // it with the handle. At this point, the `HANDLE` should no longer
  // be closed via `CloseHandle`, but instead close the returned `int`
  // with `_close`. This method should almost never be used, and
  // exists only for compatibility with 3rdparty dependencies.
  int crt() const
  {
    CHECK_EQ(Type::HANDLE, type());
    // TODO(andschwa): Consider if we should overwrite `handle_`.
    return ::_open_osfhandle(reinterpret_cast<intptr_t>(handle_), O_RDWR);
  }

  operator HANDLE() const
  {
    // A `SOCKET` can be treated as a regular file `HANDLE` [1]. There are
    // many Win32 functions that work on a `SOCKET` but have the `HANDLE`
    // type as a function parameter like `CreateIoCompletionPort`, so we need
    // to be able to cast a `SOCKET` based `int_fd` to a `HANDLE`.
    //
    // [1]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740522(v=vs.85).aspx // NOLINT(whitespace/line_length)
    if (type() == Type::SOCKET) {
      return reinterpret_cast<HANDLE>(socket_);
    }
    return handle_;
  }

  operator SOCKET() const
  {
    CHECK_EQ(Type::SOCKET, type());
    return socket_;
  }

  // On Windows, libevent's `evutil_socket_t` is set to `intptr_t`.
  operator intptr_t() const
  {
    CHECK_EQ(Type::SOCKET, type());
    return static_cast<intptr_t>(socket_);
  }

  Type type() const { return type_; }

  bool is_overlapped() const { return overlapped_; }

private:
  Type type_;

  union
  {
    HANDLE handle_;
    SOCKET socket_;
  };

  bool overlapped_;

  // NOTE: This function is provided only for checking validity, thus
  // it is private. It provides a view of a `WindowsFD` as an `int`.
  //
  // TODO(andschwa): Fix all uses of this conversion to use `is_valid`
  // directly instead, then remove the comparison operators. This
  // would require writing an `int_fd` class for POSIX too, instead of
  // just using `int`.
  int get_valid() const
  {
    if (is_valid()) {
      return 0;
    } else {
      return -1;
    }
  }

  // NOTE: These operators are used solely to support checking a
  // `WindowsFD` against e.g. -1 or 0 for validity. Nothing else
  // should have access to `get_valid()`.
  friend bool operator<(int left, const WindowsFD& right);
  friend bool operator<(const WindowsFD& left, int right);
  friend bool operator>(int left, const WindowsFD& right);
  friend bool operator>(const WindowsFD& left, int right);
  friend bool operator<=(int left, const WindowsFD& right);
  friend bool operator<=(const WindowsFD& left, int right);
  friend bool operator>=(int left, const WindowsFD& right);
  friend bool operator>=(const WindowsFD& left, int right);
  friend bool operator==(int left, const WindowsFD& right);
  friend bool operator==(const WindowsFD& left, int right);
  friend bool operator!=(int left, const WindowsFD& right);
  friend bool operator!=(const WindowsFD& left, int right);
};

inline std::ostream& operator<<(std::ostream& stream, const WindowsFD::Type& fd)
{
  switch (fd) {
    case WindowsFD::Type::HANDLE: {
      stream << "WindowsFD::Type::HANDLE";
      return stream;
    }
    case WindowsFD::Type::SOCKET: {
      stream << "WindowsFD::Type::SOCKET";
      return stream;
    }
    default: {
      stream << "WindowsFD::Type::UNKNOWN";
      return stream;
    }
  }
}

inline std::ostream& operator<<(std::ostream& stream, const WindowsFD& fd)
{
  stream << fd.type() << "=";
  switch (fd.type()) {
    case WindowsFD::Type::HANDLE: {
      stream << static_cast<HANDLE>(fd);
      return stream;
    }
    case WindowsFD::Type::SOCKET: {
      stream << static_cast<SOCKET>(fd);
      return stream;
    }
    default: {
      stream << "UNKNOWN";
      return stream;
    }
  }
}

// NOTE: The following operators implement all the comparisons
// possible a `WindowsFD` type and an `int`. The point of this is that
// the `WindowsFD` type must act like an `int` for compatibility
// reasons (e.g. checking validity through `fd < 0`), without actually
// being castable to an `int` to avoid ambiguous types.
inline bool operator<(int left, const WindowsFD& right)
{
  return left < right.get_valid();
}

inline bool operator<(const WindowsFD& left, int right)
{
  return left.get_valid() < right;
}

inline bool operator>(int left, const WindowsFD& right)
{
  return left > right.get_valid();
}

inline bool operator>(const WindowsFD& left, int right)
{
  return left.get_valid() > right;
}

inline bool operator<=(int left, const WindowsFD& right)
{
  return left <= right.get_valid();
}

inline bool operator<=(const WindowsFD& left, int right)
{
  return left.get_valid() <= right;
}

inline bool operator>=(int left, const WindowsFD& right)
{
  return left >= right.get_valid();
}

inline bool operator>=(const WindowsFD& left, int right)
{
  return left.get_valid() >= right;
}

inline bool operator==(int left, const WindowsFD& right)
{
  return left == right.get_valid();
}

inline bool operator==(const WindowsFD& left, int right)
{
  return left.get_valid() == right;
}

inline bool operator!=(int left, const WindowsFD& right)
{
  return left != right.get_valid();
}

inline bool operator!=(const WindowsFD& left, int right)
{
  return left.get_valid() != right;
}

// NOTE: This operator exists so that `WindowsFD` can be used in an
// `unordered_map` (and other STL containers requiring equality).
inline bool operator==(const WindowsFD& left, const WindowsFD& right)
{
  // This is `true` even if the types mismatch because we want
  // `WindowsFD(-1)` to compare as equivalent to an invalid `HANDLE`
  // or `SOCKET`, even though it is technically of type `HANDLE`.
  if (!left.is_valid() && !right.is_valid()) {
    return true;
  }

  // Otherwise mismatched types are not equivalent.
  if (left.type() != right.type()) {
    return false;
  }

  switch (left.type()) {
    case WindowsFD::Type::HANDLE: {
      return static_cast<HANDLE>(left) == static_cast<HANDLE>(right);
    }
    case WindowsFD::Type::SOCKET: {
      return static_cast<SOCKET>(left) == static_cast<SOCKET>(right);
    }
  }

  UNREACHABLE();
}

} // namespace os {

namespace std {

// NOTE: This specialization exists so that `WindowsFD` can be used in
// an `unordered_map` (and other STL containers requiring a hash).
template <>
struct hash<os::WindowsFD>
{
  using argument_type = os::WindowsFD;
  using result_type = size_t;

  result_type operator()(const argument_type& fd) const noexcept
  {
    switch (fd.type()) {
      case os::WindowsFD::Type::HANDLE: {
        return std::hash<HANDLE>{}(static_cast<HANDLE>(fd));
      }
      case os::WindowsFD::Type::SOCKET: {
        return std::hash<SOCKET>{}(static_cast<SOCKET>(fd));
      }
    }

    UNREACHABLE();
  }
};

} // namespace std {

#endif // __STOUT_OS_WINDOWS_FD_HPP__
<|endoftext|>


<commit_before>#ifndef RADIUMENGINE_TASK_QUEUE_HPP_
#define RADIUMENGINE_TASK_QUEUE_HPP_

#include <Core/CoreMacros.hpp>

#include <memory>
#include <vector>
#include <deque>
#include <thread>
#include <mutex>

namespace Ra { namespace Core { class Task; } }

namespace Ra { namespace Core
{

    class TaskQueue
    {
    public:
        typedef uint TaskId;
        enum { InvalidTaskId = TaskId(-1) };
    public:
        // Constructor and destructor.
        TaskQueue(int numThreads);
        ~TaskQueue();

        // Note : functions are not thread safe and should only be called from the main thread.

        /// Registers a task to be executed.
        /// Task must have been created with new and be initialized with its parameter.
        /// The task queue assumes ownership of the task.
        TaskId registerTask(Task * task);

        /// Add dependency between two tasks. A task will be executed only when all
        /// its dependencies are satisfied.
        void addDependency(TaskId predecessor, TaskId successor);

        /// Puts the task on the queue to be executed. A task can only be queued if it has
        /// no dependencies.
        void queueTask(TaskId task);

        /// Executes the task queue. Blocks until all tasks in queue and dependencies are finished.
        void processTaskQueue();

        /// Erases all tasks. Will assert if tasks are unprocessed.
        void flushTaskQueue();

    private:
        /// Function called by a new thread given a new task.
        void runTask(TaskId task);

    private:
        /// Maximum number of concurently running tasks.
        const uint m_numThreads;
        /// Storage for the tasks (task will be deleted
        std::vector< std::unique_ptr<Task> > m_tasks;
        /// For each task, stores which tasks depend on it.
        std::vector< std::vector <TaskId> > m_dependencies;

        // mutex protected variables.

        /// Global mutex over thread-sensitive variables.
        std::mutex m_taskQueueMutex;
        /// Number of tasks each task is waiting on.
        std::vector<uint> m_remainingDependencies;
        /// Queue holding the pending tasks.
        std::deque<TaskId> m_taskQueue;
        /// Number of tasks currently being processed.
        uint m_processingTasks;

    };

}}

#endif // RADIUMENGINE_TASK_QUEUE_HPP_<commit_msg>Minor comment fix.<commit_after>#ifndef RADIUMENGINE_TASK_QUEUE_HPP_
#define RADIUMENGINE_TASK_QUEUE_HPP_

#include <Core/CoreMacros.hpp>

#include <memory>
#include <vector>
#include <deque>
#include <thread>
#include <mutex>

namespace Ra { namespace Core { class Task; } }

namespace Ra { namespace Core
{

    class TaskQueue
    {
    public:
        typedef uint TaskId;
        enum { InvalidTaskId = TaskId(-1) };
    public:
        // Constructor and destructor.
        TaskQueue(int numThreads);
        ~TaskQueue();

        // Note : functions are not thread safe and should only be called from the main thread.

        /// Registers a task to be executed.
        /// Task must have been created with new and be initialized with its parameter.
        /// The task queue assumes ownership of the task.
        TaskId registerTask(Task * task);

        /// Add dependency between two tasks. A task will be executed only when all
        /// its dependencies are satisfied.
        void addDependency(TaskId predecessor, TaskId successor);

        /// Puts the task on the queue to be executed. A task can only be queued if it has
        /// no dependencies.
        void queueTask(TaskId task);

        /// Executes the task queue. Blocks until all tasks in queue and dependencies are finished.
        void processTaskQueue();

        /// Erases all tasks. Will assert if tasks are unprocessed.
        void flushTaskQueue();

    private:
        /// Function called by a new thread given a new task.
        void runTask(TaskId task);

    private:
        /// Maximum number of concurently running tasks.
        const uint m_numThreads;
        /// Storage for the tasks (task will be deleted by flushQueue)
        std::vector< std::unique_ptr<Task> > m_tasks;
        /// For each task, stores which tasks depend on it.
        std::vector< std::vector <TaskId> > m_dependencies;

        // mutex protected variables.

        /// Global mutex over thread-sensitive variables.
        std::mutex m_taskQueueMutex;
        /// Number of tasks each task is waiting on.
        std::vector<uint> m_remainingDependencies;
        /// Queue holding the pending tasks.
        std::deque<TaskId> m_taskQueue;
        /// Number of tasks currently being processed.
        uint m_processingTasks;

    };

}}

#endif // RADIUMENGINE_TASK_QUEUE_HPP_
<|endoftext|>


<commit_before>//-----------------------------------------------------------------------bl-
//--------------------------------------------------------------------------
//
// QUESO - a library to support the Quantification of Uncertainty
// for Estimation, Simulation and Optimization
//
// Copyright (C) 2008,2009,2010,2011,2012,2013 The PECOS Development Team
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Version 2.1 GNU Lesser General
// Public License as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc. 51 Franklin Street, Fifth Floor,
// Boston, MA  02110-1301  USA
//
//-----------------------------------------------------------------------el-

/*
 * Brief description of this file:
 *
 * This file contains the code for the user defined qoi routine.
 */

#include <queso/GslVector.h>
#include <queso/GslMatrix.h>
#include <gravity_qoi.h>

template<class P_V, class P_M, class Q_V, class Q_M>
Qoi<P_V, P_M, Q_V, Q_M>::Qoi(const char * prefix,
    const QUESO::VectorSet<P_V, P_M> & domainSet,
    const QUESO::VectorSet<Q_V, Q_M> & imageSet)
  : QUESO::BaseVectorFunction<P_V, P_M, Q_V, Q_M>(prefix, domainSet, imageSet),
    m_angle(M_PI / 4.0),
    m_initialVelocity(5.0),
    m_initialHeight(0.0)
{
}

template<class P_V, class P_M, class Q_V, class Q_M>
Qoi<P_V, P_M, Q_V, Q_M>::~Qoi()
{
  // Deconstruct here
}

template<class P_V, class P_M, class Q_V, class Q_M>
void
Qoi<P_V, P_M, Q_V, Q_M>::compute(const P_V & domainVector,
    const P_V * domainDirection,
    Q_V & imageVector, QUESO::DistArray<P_V *> * gradVectors,
    QUESO::DistArray<P_M *> * hessianMatrices,
    QUESO::DistArray<P_V *> * hessianEffects) const
{
  double g = domainVector[0];  // Sample of the RV 'gravity acceleration'
  double distanceTraveled = 0.0;
  double aux = m_initialVelocity * std::sin(m_angle);
  distanceTraveled = (m_initialVelocity * std::cos(m_angle) / g) *
    (aux + std::sqrt(std::pow(aux, 2) + 2.0 * g * m_initialHeight));

  imageVector[0] = distanceTraveled;
}

template class Qoi<QUESO::GslVector, QUESO::GslMatrix, QUESO::GslVector,
                   QUESO::GslMatrix>;
<commit_msg>Add back in the error messages<commit_after>//-----------------------------------------------------------------------bl-
//--------------------------------------------------------------------------
//
// QUESO - a library to support the Quantification of Uncertainty
// for Estimation, Simulation and Optimization
//
// Copyright (C) 2008,2009,2010,2011,2012,2013 The PECOS Development Team
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Version 2.1 GNU Lesser General
// Public License as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc. 51 Franklin Street, Fifth Floor,
// Boston, MA  02110-1301  USA
//
//-----------------------------------------------------------------------el-

/*
 * Brief description of this file:
 *
 * This file contains the code for the user defined qoi routine.
 */

#include <queso/GslVector.h>
#include <queso/GslMatrix.h>
#include <gravity_qoi.h>

template<class P_V, class P_M, class Q_V, class Q_M>
Qoi<P_V, P_M, Q_V, Q_M>::Qoi(const char * prefix,
    const QUESO::VectorSet<P_V, P_M> & domainSet,
    const QUESO::VectorSet<Q_V, Q_M> & imageSet)
  : QUESO::BaseVectorFunction<P_V, P_M, Q_V, Q_M>(prefix, domainSet, imageSet),
    m_angle(M_PI / 4.0),
    m_initialVelocity(5.0),
    m_initialHeight(0.0)
{
}

template<class P_V, class P_M, class Q_V, class Q_M>
Qoi<P_V, P_M, Q_V, Q_M>::~Qoi()
{
  // Deconstruct here
}

template<class P_V, class P_M, class Q_V, class Q_M>
void
Qoi<P_V, P_M, Q_V, Q_M>::compute(const P_V & domainVector,
    const P_V * domainDirection,
    Q_V & imageVector, QUESO::DistArray<P_V *> * gradVectors,
    QUESO::DistArray<P_M *> * hessianMatrices,
    QUESO::DistArray<P_V *> * hessianEffects) const
{
  if (domainVector.sizeLocal() != 1) {
    queso_error_msg("domainVector does not have size 1");
  }
  if (imageVector.sizeLocal() != 1) {
    queso_error_msg("imageVector does not have size 1");
  }

  double g = domainVector[0];  // Sample of the RV 'gravity acceleration'
  double distanceTraveled = 0.0;
  double aux = m_initialVelocity * std::sin(m_angle);
  distanceTraveled = (m_initialVelocity * std::cos(m_angle) / g) *
    (aux + std::sqrt(std::pow(aux, 2) + 2.0 * g * m_initialHeight));

  imageVector[0] = distanceTraveled;
}

template class Qoi<QUESO::GslVector, QUESO::GslMatrix, QUESO::GslVector,
                   QUESO::GslMatrix>;
<|endoftext|>


<commit_before>/**
\description The is the main for the new version of the mert algorithm develloppped during the 2nd MT marathon
*/

#include <limits>
#include "Data.h"
#include "Point.h"
#include "Scorer.h"
#include "ScoreData.h"
#include "FeatureData.h"
#include "Optimizer.h"
#include "getopt.h"
#include "Types.h"
#include <unistd.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
#include "Util.h"

float min_interval = 1e-3;

using namespace std;

void usage(void) {
  cerr<<"usage: mert -d <dimensions> (mandatory )"<<endl;
  cerr<<"[-n retry ntimes (default 1)]"<<endl;
  cerr<<"[-o\tthe indexes to optimize(default all)]"<<endl;
  cerr<<"[-t\tthe optimizer(default Powell)]"<<endl;
  cerr<<"[--sctype] the scorer type (default BLEU)"<<endl;
  cerr<<"[--scfile] the scorer data file (default score.data)"<<endl;
  cerr<<"[--ffile] the feature data file data file (default feature.data)"<<endl;
  cerr<<"[-v] verbose level"<<endl;
  exit(1);
}

static struct option long_options[] =
  {
    {"pdim", 1, 0, 'd'},
    {"ntry",1,0,'n'},
    {"optimize",1,0,'o'},
    {"type",1,0,'t'},
    {"sctype",1,0,'s'},
    {"scfile",1,0,'S'},
    {"ffile",1,0,'F'},
    {"verbose",1,0,'v'},
    {0, 0, 0, 0}
  };
int option_index;

int main (int argc, char **argv) {
  int c,pdim,i;
  pdim=-1;
  int ntry=1;
  string type("powell");
  string scorertype("BLEU");
  string scorerfile("statscore.data");
  string featurefile("features.data");
  vector<unsigned> tooptimize;
  vector<parameter_t> start;
  while ((c=getopt_long (argc, argv, "d:n:t:s:S:F:v:", long_options, &option_index)) != -1) {
    switch (c) {
    case 'd':
      pdim = strtol(optarg, NULL, 10);
      break;
    case 'n':
      ntry=strtol(optarg, NULL, 10);
      break;
    case 't':
      type=string(optarg);
      break;
      case's':
	scorertype=string(optarg);
      break;
    case 'S':
      scorerfile=string(optarg);
    case 'F':
      featurefile=string(optarg);
      break;
    case 'v':
      setverboselevel(strtol(optarg,NULL,10));
      break;
    default:
      usage();
    }
  }
  if (pdim < 0)
    usage();
  if(tooptimize.empty()){
    tooptimize.resize(pdim);//We'll optimize on everything
    for(i=0;i<pdim;i++)
      tooptimize[i]=i;
  }
  ifstream opt("init.opt");
  if(opt.fail()){
    cerr<<"could not open init.opt"<<endl;
    exit(3);
  }
  start.resize(pdim);//to do:read from file
  int j;
  for( j=0;j<pdim&&!opt.fail();j++)
    opt>>start[j];
  if(j<pdim){
    cerr<<"error could not initialize start point with init.opt"<<endl;
    exit(3);
  }

  opt.close();
  //it make sense to know what parameter set were used to generate the nbest
  ScorerFactory SF;
  Scorer *TheScorer=SF.getScorer(scorertype);
  ScoreData *SD=new ScoreData(*TheScorer);
  SD->load(scorerfile);
  FeatureData *FD=new FeatureData();
  FD->load(featurefile);
  Optimizer *O=OptimizerFactory::BuildOptimizer(pdim,tooptimize,start,type);
  O->SetScorer(TheScorer);
  O->SetFData(FD);
  Point P(start);//Generate from the full feature set. Warning: must ne done after Optimiezr initialiazation
  Point bestP=P;
  statscore_t best=O->Run(P);
  statscore_t mean=best;
  statscore_t var=best*best;

  vector<parameter_t> min(Point::getdim());
  vector<parameter_t> max(Point::getdim());

 for(int d=0;d<Point::getdim();d++){
    min[d]=0.0;
    max[d]=1.0;
  }
  //note: those mins and max are the bound for the starting points of the algorithm, not strict bound on the result!

 for(int i=1;i<ntry;i++){
   P.Randomize(min,max);
   statscore_t score=O->Run(P);
   if(score>best){
     best=score;
     bestP=P;
  }
   mean+=score;
   var+=(score*score);
 }
 mean/=(float)ntry;
 var/=(float)ntry;
 var=sqrt(abs(var-mean*mean));
 if(ntry>1)
   cerr<<"variance of the score (for "<<ntry<<" try):"<<var<<endl;
 cerr<<"best score"<<best<<endl;
 ofstream res("weights.txt");
 res<<bestP<<endl;
}
<commit_msg>Save correct weight set!<commit_after>/**
\description The is the main for the new version of the mert algorithm develloppped during the 2nd MT marathon
*/

#include <limits>
#include "Data.h"
#include "Point.h"
#include "Scorer.h"
#include "ScoreData.h"
#include "FeatureData.h"
#include "Optimizer.h"
#include "getopt.h"
#include "Types.h"
#include <unistd.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
#include "Util.h"

float min_interval = 1e-3;

using namespace std;

void usage(void) {
  cerr<<"usage: mert -d <dimensions> (mandatory )"<<endl;
  cerr<<"[-n retry ntimes (default 1)]"<<endl;
  cerr<<"[-o\tthe indexes to optimize(default all)]"<<endl;
  cerr<<"[-t\tthe optimizer(default Powell)]"<<endl;
  cerr<<"[--sctype] the scorer type (default BLEU)"<<endl;
  cerr<<"[--scfile] the scorer data file (default score.data)"<<endl;
  cerr<<"[--ffile] the feature data file data file (default feature.data)"<<endl;
  cerr<<"[-v] verbose level"<<endl;
  exit(1);
}

static struct option long_options[] =
  {
    {"pdim", 1, 0, 'd'},
    {"ntry",1,0,'n'},
    {"optimize",1,0,'o'},
    {"type",1,0,'t'},
    {"sctype",1,0,'s'},
    {"scfile",1,0,'S'},
    {"ffile",1,0,'F'},
    {"verbose",1,0,'v'},
    {0, 0, 0, 0}
  };
int option_index;

int main (int argc, char **argv) {
  int c,pdim,i;
  pdim=-1;
  int ntry=1;
  string type("powell");
  string scorertype("BLEU");
  string scorerfile("statscore.data");
  string featurefile("features.data");
  vector<unsigned> tooptimize;
  vector<parameter_t> start;
  while ((c=getopt_long (argc, argv, "d:n:t:s:S:F:v:", long_options, &option_index)) != -1) {
    switch (c) {
    case 'd':
      pdim = strtol(optarg, NULL, 10);
      break;
    case 'n':
      ntry=strtol(optarg, NULL, 10);
      break;
    case 't':
      type=string(optarg);
      break;
      case's':
	scorertype=string(optarg);
      break;
    case 'S':
      scorerfile=string(optarg);
    case 'F':
      featurefile=string(optarg);
      break;
    case 'v':
      setverboselevel(strtol(optarg,NULL,10));
      break;
    default:
      usage();
    }
  }
  if (pdim < 0)
    usage();
  if(tooptimize.empty()){
    tooptimize.resize(pdim);//We'll optimize on everything
    for(i=0;i<pdim;i++)
      tooptimize[i]=i;
  }
  ifstream opt("init.opt");
  if(opt.fail()){
    cerr<<"could not open init.opt"<<endl;
    exit(3);
  }
  start.resize(pdim);//to do:read from file
  int j;
  for( j=0;j<pdim&&!opt.fail();j++)
    opt>>start[j];
  if(j<pdim){
    cerr<<"error could not initialize start point with init.opt"<<endl;
    exit(3);
  }

  opt.close();
  //it make sense to know what parameter set were used to generate the nbest
  ScorerFactory SF;
  Scorer *TheScorer=SF.getScorer(scorertype);
  ScoreData *SD=new ScoreData(*TheScorer);
  SD->load(scorerfile);
  FeatureData *FD=new FeatureData();
  FD->load(featurefile);
  Optimizer *O=OptimizerFactory::BuildOptimizer(pdim,tooptimize,start,type);
  O->SetScorer(TheScorer);
  O->SetFData(FD);
  Point P(start);//Generate from the full feature set. Warning: must ne done after Optimiezr initialiazation
  statscore_t best=O->Run(P);
  Point bestP=P;
  statscore_t mean=best;
  statscore_t var=best*best;

  vector<parameter_t> min(Point::getdim());
  vector<parameter_t> max(Point::getdim());

 for(int d=0;d<Point::getdim();d++){
    min[d]=0.0;
    max[d]=1.0;
  }
  //note: those mins and max are the bound for the starting points of the algorithm, not strict bound on the result!

 for(int i=1;i<ntry;i++){
   P.Randomize(min,max);
   statscore_t score=O->Run(P);
   if(score>best){
     best=score;
     bestP=P;
  }
   mean+=score;
   var+=(score*score);
 }
 mean/=(float)ntry;
 var/=(float)ntry;
 var=sqrt(abs(var-mean*mean));
 if(ntry>1)
   cerr<<"variance of the score (for "<<ntry<<" try):"<<var<<endl;
 cerr<<"best score"<<best<<endl;
 ofstream res("weights.txt");
 res<<bestP<<endl;
}
<|endoftext|>


<commit_before>/**
 * @file    snake.cpp
 * @brief   Arquivo com as funcoes que implementam
            a resolucao do labirinto.
 * @author  Daniel Barbosa (nome@email.com)
 * @author  Jaine Budke (jainebudke@hotmail.com)
 * @since   30/05/2017
 * @date    19/06/2017
 */

#include "snake.h"

/** @brief Tenta encontrar caminho para chegar na maçã.
    @return 1 se for possível, 0 se for impossível. */
bool Snake::solveMaze(){

    // TODO
    // FAZER ESSA PARTE ACHANDO REALMENTE DIRECAO VALIDA PARA A SNAKE

    // recuperar a posicao inicial automaticamente tbm

    /*
    Game::Position posInicial;
    posInicial.x = 3;  // coluna
    posInicial.y = 5;  // linha

    Game::Direction dir1;
    Game::Direction dir2;

    dir1.x = posInicial.x + 1;
    dir1.y = posInicial.y;

    dir2.x = posInicial.x + 2;
    dir2.y = dir1.y;

    listDirections.push_back( dir1 );
    listDirections.push_back( dir2 );
    */

    // VER JEITO DE RECUPERAR ISSO
    //std::vector<Game::Position> tamanhos = gm.getSizeBoards();
    //Game::Position tam = tamanhos[ gm.getCurrentLevel() ];

    int map[13][44];  // criando um mapa do labirinto
    for( int i=0 ; i < 13 ; i++ ){
        for( int j=0 ; j < 44 ; j++ ){
            map[i][j] = 0;
        }
    }

    for( int i=0 ; i < 13 ; i++ ){
        map[i][0] = 1;
        map[i][43] = 1;
    }

    for( int j=0 ; j < 44 ; j++ ){
        map[0][j] = 1;
        map[12][j] = 1;
    }

    Game::Position maca;
    maca.y = 6;
    maca.x = 38;

    int visited = 110;

    Game::Position posInicial;
    posInicial.x = 3;  // coluna
    posInicial.y = 5;  // linha

    std::stack< Game::Position > marked;

    Game::Position currentPosition;

    // Tornar initialPosition a celula atual e marcar como visitada
    currentPosition = posInicial;
    map[posInicial.y][posInicial.x] = 1;
    visited += 1;

    // buscar celulas ainda não visitadas
    while( visited <= 44*13 ){

        // verifica se os vizinhos estao livres ou tem maçã
        // Vizinhos:
        Game::Position north, south, east, west;
        north.x = currentPosition.x;     // persiste coluna
        if( (currentPosition.y)+1 == 0 or (currentPosition.y)+1 == 1 ){
            north.y = (currentPosition.y)+1;  // incrementa linha
        } else {
            north.x = 0;
            north.y = 0;
        }

        south.x = currentPosition.x;     // persiste coluna
        if( (currentPosition.y)-1 == 0 or (currentPosition.y)-1 == 1 ){
            south.y = (currentPosition.y)-1; // decrementa linha
        } else {
            south.x = 0;
            south.y = 0;
        }

        if( (currentPosition.x)-1 == 0 or (currentPosition.x)-1 == 1 ){
            east.x = (currentPosition.x)-1; // decrementa coluna
        } else {
            east.x = 0;
            east.y = 0;
        }
        east.y = currentPosition.y;     // persiste linha

        if( (currentPosition.x)+1 == 0 or (currentPosition.x)+1 == 1 ){
            west.x = (currentPosition.x)+1; // incrementa coluna
        } else {
            west.x = 0;
            west.y = 0;
        }
        west.y = currentPosition.y;     // persiste linha

        if(    map[north.y][north.x] == 0
            or map[south.y][south.x] == 0
            or map[east.y][east.x]   == 0
            or map[west.y][west.x]   == 0 ){ // vizinhos não sao parede nem foram visitados

            // escolhe um dos vizinhos
            Game::Position vizinho;
            if( map[north.y][north.x] == 0 ){
                vizinho = north;
                visited += 1;
                map[north.y][north.x] = 1;
                std::cout << "TESTE1\n";
            } else if( map[south.y][south.x] == 0 ){
                vizinho = south;
                visited += 1;
                map[south.y][south.x] = 1;
                std::cout << "TESTE2\n";
            } else if( map[east.y][east.x]   == 0 ){
                vizinho = east;
                visited += 1;
                map[east.y][east.x] = 1;
                std::cout << "TESTE3\n";
            } else if( map[west.y][west.x]   == 0 ){
                vizinho = west;
                visited += 1;
                map[west.y][west.x] = 1;
                std::cout << "TESTE4\n";
            }

            std::cout << "TESTE5\n";

            // dá o push da posicao atual pra pilha
            marked.push( currentPosition );

            // posicao atual agora é o vizinho escolhido
            currentPosition = vizinho;

        } else if( !(marked.empty()) ){ // se a pilha não estiver vazia

            // tira o ultimo elemento da pilha
            marked.pop();

            // vê e salva quem é o proximo
            Game::Position newPosition = marked.top();

            // torna nova célula a atual
            currentPosition = newPosition;
            std::cout << "TESTE6\n";

        } else if( currentPosition.x == maca.x and currentPosition.y == maca.y ){

            while( !(marked.empty()) ){
                // ve quem é o prox
                Game::Position dir = marked.top();

                std::cout << dir.y << " ; " <<dir.x << " - ";
                // add na lista
                listDirections.push_back( dir );
                // tira ele
                marked.pop();

            }
            std::cout << "TESTE7\n";

            return true;
        }

    }

    return false;

}<commit_msg>snaze<commit_after>/**
 * @file    snake.cpp
 * @brief   Arquivo com as funcoes que implementam
            a resolucao do labirinto.
 * @author  Daniel Barbosa (nome@email.com)
 * @author  Jaine Budke (jainebudke@hotmail.com)
 * @since   30/05/2017
 * @date    19/06/2017
 */

#include "snake.h"

/** @brief Tenta encontrar caminho para chegar na maçã.
    @return 1 se for possível, 0 se for impossível. */
bool Snake::solveMaze(){

    // TODO
    // FAZER ESSA PARTE ACHANDO REALMENTE DIRECAO VALIDA PARA A SNAKE

    // recuperar a posicao inicial automaticamente tbm

    /*
    Game::Position posInicial;
    posInicial.x = 3;  // coluna
    posInicial.y = 5;  // linha

    Game::Position dir1;
    Game::Position dir2;

    dir1.x = posInicial.x + 1;
    dir1.y = posInicial.y;

    dir2.x = posInicial.x + 2;
    dir2.y = dir1.y;

    listDirections.push_back( dir1 );
    listDirections.push_back( dir2 );
    */

    // VER JEITO DE RECUPERAR ISSO
    //std::vector<Game::Position> tamanhos = gm.getSizeBoards();
    //Game::Position tam = tamanhos[ gm.getCurrentLevel() ];

    int map[13][44];  // criando um mapa do labirinto
    for( int i=0 ; i < 13 ; i++ ){
        for( int j=0 ; j < 44 ; j++ ){
            map[i][j] = 0;
        }
    }

    for( int i=0 ; i < 13 ; i++ ){
        map[i][0] = 1;
        map[i][43] = 1;
    }

    for( int j=0 ; j < 44 ; j++ ){
        map[0][j] = 1;
        map[12][j] = 1;
    }

    Game::Position maca;
    maca.y = 6;
    maca.x = 38;

    int visited = 110;

    Game::Position posInicial;
    posInicial.x = 3;  // coluna
    posInicial.y = 5;  // linha

    std::stack< Game::Position > marked;

    Game::Position currentPosition;

    // Tornar initialPosition a celula atual e marcar como visitada
    currentPosition = posInicial;
    map[posInicial.y][posInicial.x] = 1;
    visited += 1;

    // buscar celulas ainda não visitadas
    while( visited <= 44*13 ){

        // verifica se os vizinhos estao livres ou tem maçã
        // Vizinhos:
        Game::Position north, south, east, west;
        north.x = currentPosition.x;     // persiste coluna
        if( (currentPosition.y)+1 == 0 or (currentPosition.y)+1 == 1 ){
            north.y = (currentPosition.y)+1;  // incrementa linha
        } else {
            north.x = 0;
            north.y = 0;
        }

        south.x = currentPosition.x;     // persiste coluna
        if( (currentPosition.y)-1 == 0 or (currentPosition.y)-1 == 1 ){
            south.y = (currentPosition.y)-1; // decrementa linha
        } else {
            south.x = 0;
            south.y = 0;
        }

        if( (currentPosition.x)-1 == 0 or (currentPosition.x)-1 == 1 ){
            east.x = (currentPosition.x)-1; // decrementa coluna
        } else {
            east.x = 0;
            east.y = 0;
        }
        east.y = currentPosition.y;     // persiste linha

        if( (currentPosition.x)+1 == 0 or (currentPosition.x)+1 == 1 ){
            west.x = (currentPosition.x)+1; // incrementa coluna
        } else {
            west.x = 0;
            west.y = 0;
        }
        west.y = currentPosition.y;     // persiste linha

        if(    map[north.y][north.x] == 0
            or map[south.y][south.x] == 0
            or map[east.y][east.x]   == 0
            or map[west.y][west.x]   == 0 ){ // vizinhos não sao parede nem foram visitados

            // escolhe um dos vizinhos
            Game::Position vizinho;
            if( map[north.y][north.x] == 0 ){
                vizinho = north;
                visited += 1;
                map[north.y][north.x] = 1;
                std::cout << "TESTE1\n";
            } else if( map[south.y][south.x] == 0 ){
                vizinho = south;
                visited += 1;
                map[south.y][south.x] = 1;
                std::cout << "TESTE2\n";
            } else if( map[east.y][east.x]   == 0 ){
                vizinho = east;
                visited += 1;
                map[east.y][east.x] = 1;
                std::cout << "TESTE3\n";
            } else if( map[west.y][west.x]   == 0 ){
                vizinho = west;
                visited += 1;
                map[west.y][west.x] = 1;
                std::cout << "TESTE4\n";
            }

            std::cout << "TESTE5\n";

            // dá o push da posicao atual pra pilha
            marked.push( currentPosition );

            // posicao atual agora é o vizinho escolhido
            currentPosition = vizinho;

        } else if( !(marked.empty()) ){ // se a pilha não estiver vazia

            // tira o ultimo elemento da pilha
            marked.pop();

            // vê e salva quem é o proximo
            Game::Position newPosition = marked.top();

            // torna nova célula a atual
            currentPosition = newPosition;
            std::cout << "TESTE6\n";

        } else if( currentPosition.x == maca.x and currentPosition.y == maca.y ){

            while( !(marked.empty()) ){
                // ve quem é o prox
                Game::Position dir = marked.top();

                std::cout << dir.y << " ; " <<dir.x << " - ";
                // add na lista
                listDirections.push_back( dir );
                // tira ele
                marked.pop();

            }
            std::cout << "TESTE7\n";

            return true;
        }

    }

    return false;

}<|endoftext|>