text:
// Copyright (C) 2013-2014 DNAnexus, Inc.
//
// This file is part of dx-toolkit (DNAnexus platform client libraries).
//
//   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.

#include "file.h"
#include "dxcpp/dxcpp.h"
#include "options.h"

#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>

namespace fs = boost::filesystem;

#include "api_helper.h"
#include "dxcpp/dxlog.h"

using namespace std;
using namespace dx;

string File::createResumeInfoString(const int64_t fileSize, const int64_t modifiedTimestamp, const bool toCompress, const int64_t chunkSize, const string &path) {
  using namespace boost;
  string toReturn;
  toReturn += lexical_cast<string>(fileSize) + " ";
  toReturn += lexical_cast<string>(modifiedTimestamp) + " ";
  toReturn += lexical_cast<string>(toCompress) + " ";
  toReturn += lexical_cast<string>(chunkSize) + " ";
  toReturn += path;
  return toReturn;
}

void testLocalFileExists(const string &filename) {
  DXLOG(logINFO) << "Testing existence of local file " << filename << "...";
  fs::path p(filename);
  if (fs::exists(p)) {
    DXLOG(logINFO) << " success.";
  } else {
    DXLOG(logINFO) << " failure.";
    throw runtime_error("Local file " + filename + " does not exist.");
  }
}

int numberOfCompletedParts(const dx::JSON &parts) {
  int64_t numParts = 0;
  for (dx::JSON::const_object_iterator it = parts.object_begin(); it != parts.object_end(); ++it) {
    if (it->second["state"].get<string>() == "complete") {
      numParts++;
    }
  }
  return numParts;
}

/* Returns percentage of the file already uploaded. */
double percentageComplete(const dx::JSON &parts, const int64_t size, const int64_t chunkSize) {
  if (size == 0) {
    return ((parts.has("1") && parts["1"]["state"].get<string>() == "complete") ? 100.0 : 0.0);
  }
  const int completed = numberOfCompletedParts(parts);
  const int lastPartIndex = ((size % chunkSize) == 0) ? int(size / chunkSize) : int(ceil(double(size) / chunkSize));
  bool lastPartDone = false;
  if (parts.has(boost::lexical_cast<string>(lastPartIndex)) && parts[boost::lexical_cast<string>(lastPartIndex)]["state"].get<string>() == "complete") {
    lastPartDone = true;
  }
  const int64_t lastPartSize = size % chunkSize;
  const int64_t totalBytesUploaded = (lastPartDone) ? (((completed - 1) * chunkSize) + lastPartSize) :  (completed * chunkSize);
  return (double(totalBytesUploaded) / size) * 100.0;
}

File::File(const string &localFile_, const string &projectSpec_, const string &folder_, const string &name_,
           const bool toCompress_, const bool tryResuming, const string &mimeType_,
           const int64_t chunkSize_, const unsigned fileIndex_)
  : localFile(localFile_), projectSpec(projectSpec_), folder(folder_), name(name_),
    failed(false), waitOnClose(false), closed(false), toCompress(toCompress_), mimeType(mimeType_),
    chunkSize(chunkSize_), bytesUploaded(0), fileIndex(fileIndex_), atleastOnePartDone(false), jobID() {

  init(tryResuming);
}

void File::init(const bool tryResuming) {
  projectID = resolveProject(projectSpec);

  testLocalFileExists(localFile);

  fs::path p(localFile);
  size = fs::file_size(p);
  if (size == 0) {
    // Never try to compress empty file!
    toCompress = false;
  }
  string remoteFileName = name;

  if (toCompress)
    remoteFileName += ".gz";

  const int64_t modifiedTimestamp = static_cast<int64_t>(fs::last_write_time(p));
  dx::JSON properties(dx::JSON_OBJECT);

  // Add property {FILE_SIGNATURE_PROPERTY: "<size> <modified time stamp> <toCompress> <chunkSize> <name of file>"
  properties[FILE_SIGNATURE_PROPERTY] = File::createResumeInfoString(size, modifiedTimestamp, toCompress, chunkSize, fs::canonical(p).string());

  DXLOG(logINFO) << "Resume info string: '" << properties[FILE_SIGNATURE_PROPERTY].get<string>() << "'";
  dx::JSON findResult;
  if (tryResuming) {
    // Now check if a resumable file already exist in the project
    findResult = findResumableFileObject(projectID, properties[FILE_SIGNATURE_PROPERTY].get<string>());
    if (findResult.size() == 1) {
      fileID = findResult[0]["id"].get<string>();
      double completePercentage;
      string state = findResult[0]["describe"]["state"].get<string>();
      if (state == "closing" || state == "closed") {
        isRemoteFileOpen = false;
        bytesUploaded = size;
        completePercentage = 100.0;
      } else {
        completePercentage = percentageComplete(findResult[0]["describe"]["parts"], size, chunkSize);
        isRemoteFileOpen = true;
      }
      DXLOG(logINFO) << "A resume target is found .. " << endl;
      cerr << "Signature of file " << localFile << " matches remote file " << findResult[0]["describe"]["name"].get<string>()
           << " (" << fileID << "), which is " << completePercentage << "% complete. Will resume uploading to it." << endl;
      DXLOG(logINFO) << "Remote resume target is in state: \"" << state << "\"";
    }
    if (findResult.size() > 1) {
      cerr << endl << "More than one resumable targets for local file \"" << localFile << "\" found in the project '" + projectID + "', candidates: " << endl;
      for (unsigned i = 0; i < findResult.size(); ++i) {
        cerr << "\t" << (i + 1) << ". " << findResult[i]["describe"]["name"].get<string>() << " (" << findResult[i]["id"].get<string>() << ")" << endl;
      }
      cerr << "Unable to upload: \"" << localFile << "\"" << endl
           << "Please either clean up the potential candidate files, or run upload agent with '--do-not-resume' option" << endl;
      failed = true;
    }
  }
  if (!tryResuming || (findResult.size() == 0)) {
    // Note: It's fine if mimeType is empty string "" (since default for /file/new is anyway empty media type)
    fileID = createFileObject(projectID, folder, remoteFileName, mimeType, properties);
    isRemoteFileOpen = true;
    DXLOG(logINFO) << "fileID is " << fileID << endl;

    cerr << "Uploading file " << localFile << " to file object " << fileID;
  }
}

unsigned int File::createChunks(dx::BlockingQueue<Chunk *> &queue, const int tries) {
  if (failed || (!isRemoteFileOpen)) {
    // This is the case when:
    // 1. Multiple resumable targets exist for a file (an do-not-resume is not set).
    // 2. OR, Remote resumable target is already in "closing" or "closed" state.
    return 0;
  }
  const dx::JSON desc = dx::fileDescribe(fileID);
  // sanity check
  assert(desc["state"].get<string>() == "open");

  // Treat special case of empty file here
  if (size == 0) {
    if (desc["parts"].has("1") && desc["parts"]["1"]["state"].get<string>() == "complete") {
      DXLOG(logINFO) << "Part index 1 for fileID " << fileID << " is in complete state. Will not create an upload chunk for it.";
      atleastOnePartDone = true;
      return 0;
    }
    Chunk * c = new Chunk(localFile, fileID, 0, tries, 0, 0, toCompress, true, fileIndex);
    c->log("created");
    queue.produce(c);
    return 1;
  }

  DXLOG(logINFO) << "Creating chunks:";
  fs::path p(localFile);
  unsigned int countChunks = 0; // to iterate over chunks
  unsigned int actualChunksCreated = 0; // is not incremented for chunks which are already in "complete" state (when resuming)

  for (int64_t start = 0; start < size; start += chunkSize) {
    string partIndex = boost::lexical_cast<string>(countChunks + 1); // minimum part index is 1
    const int64_t end = min(start + chunkSize, size);
    if (desc["parts"].has(partIndex) && desc["parts"][partIndex]["state"].get<string>() == "complete") {
      DXLOG(logINFO) << "Part index " << partIndex << " for fileID " << fileID << " is in complete state. Will not create an upload chunk for it.";
      bytesUploaded += (end - start);
      atleastOnePartDone = true;
    } else {
      const bool lastChunk = ((start + chunkSize) >= size);
      Chunk * c = new Chunk(localFile, fileID, countChunks, tries, start, end, toCompress, lastChunk, fileIndex);
      c->log("created");
      queue.produce(c);
      actualChunksCreated++;
    }
    ++countChunks;
  }
  return actualChunksCreated++;
}

void File::close(void) {
  closeFileObject(fileID);
}

void File::updateState(void) {
  string state = getFileState(fileID);
  if (state == "closed") {
    DXLOG(logINFO) << "File " << fileID << " is closed.";
  }
  closed = (state == "closed");
}

ostream &operator<<(ostream &out, const File &file) {
  out << file.localFile << " (" << file.fileID << ")";
  return out;
}

max_stars_repo_name: psung/dx-toolkit

max_issues_repo_name: psung/dx-toolkit

max_forks_repo_name: psung/dx-toolkit


text:
// tokuft_engine_server_status.cpp

/*======
This file is part of Percona Server for MongoDB.

Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.

    Percona Server for MongoDB is free software: you can redistribute
    it and/or modify it under the terms of the GNU Affero General
    Public License, version 3, as published by the Free Software
    Foundation.

    Percona Server for MongoDB 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public
    License along with Percona Server for MongoDB.  If not, see
    <http://www.gnu.org/licenses/>.
======= */

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kStorage

#include <stack>

#include <boost/scoped_array.hpp>
#include <boost/static_assert.hpp>

#include <db.h>
#include <toku_time.h>
#include <partitioned_counter.h>

#include <ftcxx/db_env.hpp>

#include "mongo/base/status.h"
#include "mongo/base/string_data.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/commands/server_status.h"
#include "mongo/db/storage/tokuft/tokuft_disk_format.h"
#include "mongo/db/storage/tokuft/tokuft_engine.h"
#include "mongo/db/storage/tokuft/tokuft_engine_global_accessor.h"
#include "mongo/db/storage/tokuft/tokuft_global_options.h"
#include "mongo/util/log.h"
#include "mongo/util/mongoutils/str.h"

namespace mongo {

    class FractalTreeEngineStatus {
    public:
        class Value {
            std::string _string;
            union {
                fs_redzone_state _fsState;
                double _double;
                uint64_t _uint64;
                time_t _time;
            };
            enum Type {
                Unknown = 0,
                FilesystemState,
                String,
                UnixTime,
                TokuTime,
                UInt64,
                Double,
            };
            Type _type;

        public:
            Value() : _type(Unknown) {}

            explicit Value(const TOKU_ENGINE_STATUS_ROW_S& row) {
                if (row.type == FS_STATE) {
                    _type = FilesystemState;
                    _fsState = (fs_redzone_state) row.value.num;
                } else if (row.type == UINT64) {
                    _type = UInt64;
                    _uint64 = row.value.num;
                } else if (row.type == PARCOUNT) {
                    _type = UInt64;
                    _uint64 = read_partitioned_counter(row.value.parcount);
                } else if (row.type == UNIXTIME) {
                    _type = UnixTime;
                    _time = row.value.num;
                } else if (row.type == TOKUTIME) {
                    _type = TokuTime;
                    _double = tokutime_to_seconds(row.value.num);
                } else if (row.type == DOUBLE) {
                    _type = Double;
                    _double = row.value.dnum;
                } else if (row.type == CHARSTR) {
                    _type = String;
                    _string = row.value.str;
                } else {
                    _type = Unknown;
                }
            }

            static Value panic(uint64_t val) {
                Value v;
                v._type = UInt64;
                v._uint64 = val;
                return v;
            }

            static Value panicString(const std::string& s) {
                Value v;
                v._type = String;
                v._string = s;
                return v;
            }

            void append(BSONObjBuilder& builder, StringData name, int scale = 1) const {
                if (_type == FilesystemState) {
                    BSONObjBuilder fsBuilder(builder.subobjStart(name));
                    if (_fsState == FS_GREEN) {
                        fsBuilder.append("state", "green");
                        fsBuilder.append("msg", "");
                    } else if (_fsState == FS_YELLOW) {
                        fsBuilder.append("state", "yellow");
                        fsBuilder.append("msg",
                                         str::stream() << "Filesystem space is low: less than "
                                         << (2 * tokuftGlobalOptions.engineOptions.fsRedzone)
                                         << "% remaining.");
                    } else if (_fsState == FS_RED) {
                        fsBuilder.append("state", "red");
                        fsBuilder.append("msg",
                                         str::stream() << "Filesystem space is critical: less than "
                                         << tokuftGlobalOptions.engineOptions.fsRedzone
                                         << "% remaining.  Engine is read-only until space is freed.");
                    } else if (_fsState == FS_BLOCKED) {
                        fsBuilder.append("state", "blocked");
                        fsBuilder.append("msg", "Filesystem is completely full.");
                    } else {
                        fsBuilder.append("state", "unknown");
                        fsBuilder.append("msg", str::stream() << "Code: " << (int) _fsState);
                    }
                    fsBuilder.doneFast();
                } else if (_type == String) {
                    builder.append(name, _string);
                } else if (_type == UnixTime) {
                    builder.appendTimeT(name, _time);
                } else if (_type == TokuTime) {
                    builder.appendNumber(name, _double);
                } else if (_type == UInt64) {
                    builder.appendNumber(name, static_cast<double>(_uint64) / scale);
                } else if (_type == Double) {
                    builder.append(name, _double / scale);
                } else if (_type == Unknown) {
                    builder.append(name, "unknown");
                } else {
                    builder.append(name, "invalid");
                }
            }

            uint64_t getInteger() const {
                massert(28630, "TokuFT: wrong engine status type for getInteger", _type == UInt64);
                return _uint64;
            }

            double getDuration() const {
                if (_type == TokuTime) {
                    return _double;
                } else if (_type == UnixTime) {
                    return static_cast<double>(_time);
                }
                msgasserted(28631, "TokuFT: wrong engine status type for getDouble");
            }
        };

    private:
        typedef std::map<std::string, Value> MapType;
        MapType _map;

    public:
        FractalTreeEngineStatus(ftcxx::DBEnv& env) {
            uint64_t max_rows = env.get_engine_status_num_rows();

            boost::scoped_array<TOKU_ENGINE_STATUS_ROW_S> rows(new TOKU_ENGINE_STATUS_ROW_S[max_rows]);
            uint64_t num_rows;
            uint64_t panic;
            std::string panic_string;
            env.get_engine_status(rows.get(), max_rows, num_rows, panic, panic_string, TOKU_ENGINE_STATUS);

            _map["PANIC"] = Value::panic(panic);
            _map["PANIC_STRING"] = Value::panicString(panic_string);
            for (uint64_t i = 0; i < num_rows; ++i) {
                _map[rows[i].keyname] = Value(rows[i]);
            }
        }

        const Value& operator[](const std::string& key) const {
            MapType::const_iterator it = _map.find(key);
            if (it == _map.end()) {
                static const Value unknown;
                return unknown;
            }
            return it->second;
        }
    };

    class NestedBuilder : boost::noncopyable {
    public:
        class Stack : public std::stack<BSONObjBuilder *> {
            BSONObjBuilder _bottom;
        public:
            Stack() {
                push(&_bottom);
            }
            BSONObjBuilder &b() { return *top(); }
            operator BSONObjBuilder&() { return b(); }
        };

        NestedBuilder(Stack &stack, StringData name)
            : _stack(stack), _b(_stack.b().subobjStart(name))
        {
            _stack.push(&_b);
        }

        ~NestedBuilder() {
            _stack.pop();
            _b.doneFast();
        }

    private:
        Stack &_stack;
        BSONObjBuilder _b;
    };

    class TokuFTServerStatusSection : public ServerStatusSection {
    public:
        TokuFTServerStatusSection() : ServerStatusSection("tokuft") {}
        virtual bool includeByDefault() const { return true; }

        BSONObj generateSection(OperationContext *opCtx, const BSONElement &configElement) const {
            if (!globalStorageEngineIsTokuFT()) {
                return BSONObj();
            }

            int scale = 1;
            if (configElement.isABSONObj()) {
                BSONObj o = configElement.Obj();
                BSONElement scaleElt = o["scale"];
                if (scaleElt.ok()) {
                    scale = scaleElt.safeNumberLong();
                }
                uassert(28632, "scale must be positive", scale > 0);
            }

            NestedBuilder::Stack result;

            {
                TokuFTDiskFormatVersion diskFormatVersion(tokuftGlobalEngine()->internalMetadataDict());
                BSONObj versionObj;
                Status s = diskFormatVersion.getInfo(opCtx, versionObj);
                uassertStatusOK(s);
                result.b().append("diskFormatVersion", versionObj);
            }

            FractalTreeEngineStatus status(tokuftGlobalEnv());

            {
                NestedBuilder _n1(result, "fsync");
                status["FS_FSYNC_COUNT"].append(result, "count");
                status["FS_FSYNC_TIME"].append(result, "time");
            }
            {
                NestedBuilder _n1(result, "log");
                status["LOGGER_NUM_WRITES"].append(result, "count");
                status["LOGGER_TOKUTIME_WRITES"].append(result, "time");
                status["LOGGER_BYTES_WRITTEN"].append(result, "bytes", scale);
            }
            {
                NestedBuilder _n1(result, "cachetable");
                {
                    NestedBuilder _n2(result, "size");
                    status["CT_SIZE_CURRENT"].append(result, "current", scale);
                    status["CT_SIZE_WRITING"].append(result, "writing", scale);
                    status["CT_SIZE_LIMIT"].append(result, "limit", scale);
                }
                {
                    NestedBuilder _n2(result, "miss");
                    uint64_t fullMisses = status["CT_MISS"].getInteger();
                    // unfortunately, this is a uint64 when it's actually a tokutime...
                    double fullMisstime = tokutime_to_seconds(status["CT_MISSTIME"].getInteger());
                    uint64_t partialMisses = 0;
                    double partialMisstime = 0.0;
                    const char *partialMissKeys[] = {"FT_NUM_BASEMENTS_FETCHED_NORMAL",
                                                     "FT_NUM_BASEMENTS_FETCHED_AGGRESSIVE",
                                                     "FT_NUM_BASEMENTS_FETCHED_PREFETCH",
                                                     "FT_NUM_BASEMENTS_FETCHED_WRITE",
                                                     "FT_NUM_MSG_BUFFER_FETCHED_NORMAL",
                                                     "FT_NUM_MSG_BUFFER_FETCHED_AGGRESSIVE",
                                                     "FT_NUM_MSG_BUFFER_FETCHED_PREFETCH",
                                                     "FT_NUM_MSG_BUFFER_FETCHED_WRITE"};
                    const char *partialMisstimeKeys[] = {"FT_TOKUTIME_BASEMENTS_FETCHED_NORMAL",
                                                         "FT_TOKUTIME_BASEMENTS_FETCHED_AGGRESSIVE",
                                                         "FT_TOKUTIME_BASEMENTS_FETCHED_PREFETCH",
                                                         "FT_TOKUTIME_BASEMENTS_FETCHED_WRITE",
                                                         "FT_TOKUTIME_MSG_BUFFER_FETCHED_NORMAL",
                                                         "FT_TOKUTIME_MSG_BUFFER_FETCHED_AGGRESSIVE",
                                                         "FT_TOKUTIME_MSG_BUFFER_FETCHED_PREFETCH",
                                                         "FT_TOKUTIME_MSG_BUFFER_FETCHED_WRITE"};
                    BOOST_STATIC_ASSERT((sizeof partialMissKeys) == (sizeof partialMisstimeKeys));
                    for (size_t i = 0; i < (sizeof partialMissKeys) / (sizeof partialMissKeys[0]); ++i) {
                        partialMisses += status[partialMissKeys[i]].getInteger();
                        partialMisstime += status[partialMisstimeKeys[i]].getDuration();
                    }

                    result.b().appendNumber("count", fullMisses + partialMisses);
                    result.b().append("time", fullMisstime + partialMisstime);
                    {
                        NestedBuilder _n3(result, "full");
                        result.b().appendNumber("count", fullMisses);
                        result.b().append("time", fullMisstime);
                    }
                    {
                        NestedBuilder _n3(result, "partial");
                        result.b().appendNumber("count", partialMisses);
                        result.b().append("time", partialMisstime);
                    }
                }
                {
                    NestedBuilder _n2(result, "evictions");
                    {
                        NestedBuilder _n3(result, "partial");
                        {
                            NestedBuilder _n4(result, "nonleaf");
                            {
                                NestedBuilder _n5(result, "clean");
                                status["FT_PARTIAL_EVICTIONS_NONLEAF"].append(result, "count");
                                status["FT_PARTIAL_EVICTIONS_NONLEAF_BYTES"].append(result, "bytes", scale);
                            }
                        }
                        {
                            NestedBuilder _n4(result, "leaf");
                            {
                                NestedBuilder _n5(result, "clean");
                                status["FT_PARTIAL_EVICTIONS_LEAF"].append(result, "count");
                                status["FT_PARTIAL_EVICTIONS_LEAF_BYTES"].append(result, "bytes", scale);
                            }
                        }
                    }
                    {
                        NestedBuilder _n3(result, "full");
                        {
                            NestedBuilder _n4(result, "nonleaf");
                            {
                                NestedBuilder _n5(result, "clean");
                                status["FT_FULL_EVICTIONS_NONLEAF"].append(result, "count");
                                status["FT_FULL_EVICTIONS_NONLEAF_BYTES"].append(result, "bytes", scale);
                            }
                            {
                                NestedBuilder _n5(result, "dirty");
                                status["FT_DISK_FLUSH_NONLEAF"].append(result, "count");
                                status["FT_DISK_FLUSH_NONLEAF_UNCOMPRESSED_BYTES"].append(result, "bytes", scale);
                                status["FT_DISK_FLUSH_NONLEAF_TOKUTIME"].append(result, "time");
                            }
                        }
                        {
                            NestedBuilder _n4(result, "leaf");
                            {
                                NestedBuilder _n5(result, "clean");
                                status["FT_FULL_EVICTIONS_LEAF"].append(result, "count");
                                status["FT_FULL_EVICTIONS_LEAF_BYTES"].append(result, "bytes", scale);
                            }
                            {
                                NestedBuilder _n5(result, "dirty");
                                status["FT_DISK_FLUSH_LEAF"].append(result, "count");
                                status["FT_DISK_FLUSH_LEAF_UNCOMPRESSED_BYTES"].append(result, "bytes", scale);
                                status["FT_DISK_FLUSH_LEAF_TOKUTIME"].append(result, "time");
                            }
                        }
                    }
                }
            }
            {
                NestedBuilder _n1(result, "checkpoint");
                status["CP_CHECKPOINT_COUNT"].append(result, "count");
                status["CP_TIME_CHECKPOINT_DURATION"].append(result, "time");
                status["CP_TIME_LAST_CHECKPOINT_BEGIN"].append(result, "lastBegin");
                {
                    NestedBuilder _n2(result, "lastComplete");
                    status["CP_TIME_LAST_CHECKPOINT_BEGIN_COMPLETE"].append(result, "begin");
                    status["CP_TIME_LAST_CHECKPOINT_END"].append(result, "end");
                    status["CP_TIME_CHECKPOINT_DURATION_LAST"].append(result, "time");
                }
                {
                    NestedBuilder _n2(result, "begin");
                    status["CP_BEGIN_TIME"].append(result, "time");
                }
                {
                    NestedBuilder _n2(result, "write");
                    {
                        NestedBuilder _n3(result, "nonleaf");
                        status["FT_DISK_FLUSH_NONLEAF_FOR_CHECKPOINT"].append(result, "count");
                        status["FT_DISK_FLUSH_NONLEAF_TOKUTIME_FOR_CHECKPOINT"].append(result, "time");
                        {
                            NestedBuilder _n4(result, "bytes");
                            status["FT_DISK_FLUSH_NONLEAF_UNCOMPRESSED_BYTES_FOR_CHECKPOINT"].append(result, "uncompressed", scale);
                            status["FT_DISK_FLUSH_NONLEAF_BYTES_FOR_CHECKPOINT"].append(result, "compressed", scale);
                        }
                    }
                    {
                        NestedBuilder _n3(result, "leaf");
                        status["FT_DISK_FLUSH_LEAF_FOR_CHECKPOINT"].append(result, "count");
                        status["FT_DISK_FLUSH_LEAF_TOKUTIME_FOR_CHECKPOINT"].append(result, "time");
                        {
                            NestedBuilder _n4(result, "bytes");
                            status["FT_DISK_FLUSH_LEAF_UNCOMPRESSED_BYTES_FOR_CHECKPOINT"].append(result, "uncompressed", scale);
                            status["FT_DISK_FLUSH_LEAF_BYTES_FOR_CHECKPOINT"].append(result, "compressed", scale);
                        }
                    }
                }
            }
            {
                NestedBuilder _n1(result, "serializeTime");
                {
                    NestedBuilder _n2(result, "nonleaf");
                    status["FT_NONLEAF_SERIALIZE_TOKUTIME"].append(result, "serialize");
                    status["FT_NONLEAF_COMPRESS_TOKUTIME"].append(result, "compress");
                    status["FT_NONLEAF_DECOMPRESS_TOKUTIME"].append(result, "decompress");
                    status["FT_NONLEAF_DESERIALIZE_TOKUTIME"].append(result, "deserialize");
                }
                {
                    NestedBuilder _n2(result, "leaf");
                    status["FT_LEAF_SERIALIZE_TOKUTIME"].append(result, "serialize");
                    status["FT_LEAF_COMPRESS_TOKUTIME"].append(result, "compress");
                    status["FT_LEAF_DECOMPRESS_TOKUTIME"].append(result, "decompress");
                    status["FT_LEAF_DESERIALIZE_TOKUTIME"].append(result, "deserialize");
                }
            }
            {
                NestedBuilder _n1(result, "locktree");
                {
                    NestedBuilder _n2(result, "size");
                    status["LTM_SIZE_CURRENT"].append(result, "current", scale);
                    status["LTM_SIZE_LIMIT"].append(result, "limit", scale);
                }
            }
            {
                NestedBuilder _n1(result, "compressionRatio");
                status["FT_DISK_FLUSH_LEAF_COMPRESSION_RATIO"].append(result, "leaf");
                status["FT_DISK_FLUSH_NONLEAF_COMPRESSION_RATIO"].append(result, "nonleaf");
                status["FT_DISK_FLUSH_OVERALL_COMPRESSION_RATIO"].append(result, "overall");
            }
            {
                NestedBuilder _n1(result, "alerts");
                status["LTM_LOCK_REQUESTS_PENDING"].append(result, "locktreeRequestsPending");
                status["CP_CHECKPOINT_COUNT_FAIL"].append(result, "checkpointFailures");
                {
                    NestedBuilder _n2(result, "panic");
                    status["PANIC"].append(result, "code");
                    status["PANIC_STRING"].append(result, "msg");
                }
                {
                    NestedBuilder _n2(result, "filesystem");
                    status["FS_ENOSPC_REDZONE_STATE"].append(result, "redzone");
                    status["FS_ENOSPC_THREADS_BLOCKED"].append(result, "currentBlockedThreads");
                }
                {
                    NestedBuilder _n2(result, "longWaitEvents");
                    status["LOGGER_WAIT_BUF_LONG"].append(result, "logBufferWait");
                    {
                        NestedBuilder _n3(result, "fsync");
                        status["FS_LONG_FSYNC_COUNT"].append(result, "count");
                        status["FS_LONG_FSYNC_TIME"].append(result, "time");
                    }
                    {
                        NestedBuilder _n3(result, "cachePressure");
                        status["CT_LONG_WAIT_PRESSURE_COUNT"].append(result, "count");
                        status["CT_LONG_WAIT_PRESSURE_TIME"].append(result, "time");
                    }
                    {
                        NestedBuilder _n3(result, "checkpointBegin");
                        status["CP_LONG_BEGIN_COUNT"].append(result, "count");
                        status["CP_LONG_BEGIN_TIME"].append(result, "time");
                    }
                    {
                        NestedBuilder _n3(result, "locktreeWait");
                        status["LTM_LONG_WAIT_COUNT"].append(result, "count");
                        status["LTM_LONG_WAIT_TIME"].append(result, "time");
                    }
                    {
                        NestedBuilder _n3(result, "locktreeWaitEscalation");
                        status["LTM_LONG_WAIT_ESCALATION_COUNT"].append(result, "count");
                        status["LTM_LONG_WAIT_ESCALATION_TIME"].append(result, "time");
                    }
                }
            }

            return result.b().obj();
        }
    } tokuftServerStatusSection;

}

ext: cpp

max_stars_repo_name: Tokutek/tokumxse

max_issues_repo_name: Tokutek/tokumxse

max_forks_repo_name: Tokutek/tokumxse


text:
#include <Eigen/Dense>
#include <iostream>
#include <chrono>
#include<deformation_measures.h>

typedef std::chrono::high_resolution_clock Clock;
typedef std::vector<double> stdv;

int main(){
    /*
    ==============
    |    main    |
    ==============

    Tests different methods for computing tensor multiplications and reports the
    relative speeds.
    */

    //Deformation measures
    double J = 1.2;
    Matrix_3x3 F;
    Matrix_3x3 chi;

    //Stress measures
    Vector_9  PK2_voigt;
    Vector_9  SIGMA_voigt;
    Vector_27 M_voigt;

    Vector_9  cauchy_voigt;
    Vector_9  s_voigt;
    Vector_27 m_voigt;

    //PK2 Jacobians
    Matrix_9x9  dPK2dF;
    Matrix_9x9  dPK2dchi;
    Matrix_9x27 dPK2dgrad_chi;

    //SIGMA Jacobians
    Matrix_9x9  dSIGMAdF;
    Matrix_9x9  dSIGMAdchi;
    Matrix_9x27 dSIGMAdgrad_chi;

    //Higher order stress Jacobians
    Matrix_27x9  dMdF;
    Matrix_27x9  dMdchi;
    Matrix_27x27 dMdgrad_chi;

    //Cauchy jacobians
    Matrix_9x9  dcauchydF;
    Matrix_9x9  dcauchydchi;
    Matrix_9x27 dcauchydgrad_chi;

    //s Jacobians
    Matrix_9x9  dsdF;
    Matrix_9x9  dsdchi;
    Matrix_9x27 dsdgrad_chi;

    //m Jacobians
    Matrix_27x9  dmdF;
    Matrix_27x9  dmdchi;
    Matrix_27x27 dmdgrad_chi;

    //Set random values
    F            = Matrix_3x3::Random();
    chi          = Matrix_3x3::Random();

    PK2_voigt    = Vector_9::Random();
    SIGMA_voigt  = Vector_9::Random();
    M_voigt      = Vector_27::Random();

    cauchy_voigt = Vector_9::Random();
    s_voigt      = Vector_9::Random();
    m_voigt      = Vector_27::Random();

    dPK2dF        = Matrix_9x9::Random();
    dPK2dchi      = Matrix_9x9::Random();
    dPK2dgrad_chi = Matrix_9x27::Random();

    dSIGMAdF        = Matrix_9x9::Random();
    dSIGMAdchi      = Matrix_9x9::Random();
    dSIGMAdgrad_chi = Matrix_9x27::Random();

    dMdF        = Matrix_27x9::Random();
    dMdchi      = Matrix_27x9::Random();
    dMdgrad_chi = Matrix_27x27::Random();

    int n = 10000;

    auto t0 = Clock::now();
    auto t1 = Clock::now();

    t0 = Clock::now();
    for (int _n=0; _n<n; _n++){
        deformation_measures::map_jacobians_to_current_configuration(F, chi,
            PK2_voigt,    SIGMA_voigt, M_voigt,
            cauchy_voigt, s_voigt,     m_voigt,
            dPK2dF,       dPK2dchi,    dPK2dgrad_chi,
            dSIGMAdF,     dSIGMAdchi,  dSIGMAdgrad_chi,
            dMdF,         dMdchi,      dMdgrad_chi,
            dcauchydF,    dcauchydchi, dcauchydgrad_chi,
            dsdF,         dsdchi,      dsdgrad_chi,
            dmdF,         dmdchi,      dmdgrad_chi);
    }
    t1 = Clock::now();
    std::cout << "### Map Jacobians ###\n";
    std::cout << "   Eigen Matrices: " << std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count()/((double)n) << "\n";

    t0 = Clock::now();
    for (int _n = 0; _n<n; _n++){
        deformation_measures::map_dAdgrad_chi_to_dadgrad_chi(dMdgrad_chi, J, F, chi, dmdgrad_chi);
    }
    t1 = Clock::now();
    std::cout << "### map_dAdgrad_chi_to_dadgrad_chi ###\n";
    std::cout << "    Full nested loops: " << std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count()/((double)n) << "\n";

    t0 = Clock::now();
    for (int _n = 0; _n<n; _n++){
        deformation_measures::map_dAdgrad_chi_to_dadgrad_chi_alt(dMdgrad_chi, J, F, chi, dmdgrad_chi);
    }
    t1 = Clock::now();
    std::cout << "    Alternative nesting: " << std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count()/((double)n) << "\n";

    return 1;
}

max_stars_repo_name: lanl/tardigrade-micromorphic-element

max_issues_repo_name: lanl/tardigrade-micromorphic-element

max_forks_repo_name: lanl/tardigrade-micromorphic-element


text:
/**********************************************************\
|                                                          |
|                          hprose                          |
|                                                          |
| Official WebSite: http://www.hprose.com/                 |
|                   http://www.hprose.org/                 |
|                                                          |
\**********************************************************/

/**********************************************************\
 *                                                        *
 * ClassManager.hpp                                       *
 *                                                        *
 * hprose class manager unit for cpp.                     *
 *                                                        *
 * LastModified: Jun 15, 2014                             *
 * Author: Chen fei <cf@hprose.com>                       *
 *                                                        *
\**********************************************************/

#ifndef HPROSE_IO_CLASS_MANAGER_HPP
#define HPROSE_IO_CLASS_MANAGER_HPP

#if defined(_MSC_VER) && (_MSC_VER >= 1020)
#pragma once
#endif

#include <hprose/common.hpp>

#include <boost/tr1/unordered_map.hpp>
#include <boost/thread/shared_mutex.hpp>
#include <boost/thread/locks.hpp>

#define HPROSE_REG_CLASS(Class) \
    hprose::ClassManager::SharedInstance()->RegisterClass(&typeid(Class), #Class)
#define HPROSE_REG_CLASS_EX(Class, Alias) \
    hprose::ClassManager::SharedInstance()->RegisterClass(&typeid(Class), Alias)
#define HPROSE_REG_PROPERTY(Class, Property) \
    hprose::ClassManager::SharedInstance()->RegisterProperty(&typeid(Class), #Property, &Class::Property)
#define HPROSE_REG_PROPERTY_EX(Class, Property, Alias) \
    hprose::ClassManager::SharedInstance()->RegisterProperty(&typeid(Class), Alias, &Class::Property)

#define HproseRegClass      HPROSE_REG_CLASS
#define HproseRegClassEx    HPROSE_REG_CLASS_EX
#define HproseRegProperty   HPROSE_REG_PROPERTY
#define HproseRegPropertyEx HPROSE_REG_PROPERTY_EX

#ifndef BOOST_NO_CXX11_VARIADIC_MACROS
#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
#define HPROSE_REGISTER(Class, ...) \
    hprose::ClassManager::SharedInstance()->RegisterClass(&typeid(Class), #Class, __VA_ARGS__)

#define HproseRegister  HPROSE_REGISTER
#endif
#endif

namespace hprose {

class ClassManager {
public:

    typedef std::tr1::unordered_map<std::string, Any> ClassProperty;

public:

    inline static ClassManager * SharedInstance() {
        static ClassManager classManager;
        return &classManager;
    }

public:

#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
    template<typename... ValueTypes>
    inline void RegisterClass(const std::type_info * type, const std::string & alias, const ValueTypes &... values) {
        if (type && (!alias.empty())) {
            RegisterClass(type, alias);
            RegisterProperty(alias, values...);
        }
    }
#endif

    inline void RegisterClass(const std::type_info * type, const std::string & alias) {
        if (type && (!alias.empty())) {
#ifdef HPROSE_THREAD_SAFE_REG
            boost::unique_lock<boost::shared_mutex> lock(mutex);
#endif
            classAliases[type] = alias;
            classes[alias] = type;
        }
    }

#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
    template<typename ValueType, typename... ValueTypes>
    inline void RegisterProperty(const std::string & alias, const std::string & name, const ValueType & value, const ValueTypes &... values) {
        if ((!alias.empty()) && (!name.empty())) {
#ifdef HPROSE_THREAD_SAFE_REG
            boost::unique_lock<boost::shared_mutex> lock(propertyMutex);
#endif
            properties[alias][name] = value;
        }
        RegisterProperty(alias, values...);
    }
#endif

    template<typename ValueType>
    inline void RegisterProperty(const std::string & alias, const std::string & name, ValueType value) {
        if ((!alias.empty()) && (!name.empty())) {
#ifdef HPROSE_THREAD_SAFE_REG
            boost::unique_lock<boost::shared_mutex> lock(propertyMutex);
#endif
            properties[alias][name] = value;
        }
    }

    template<typename ValueType>
    inline void RegisterProperty(const std::type_info * type, const std::string & name, ValueType value) {
        if (type) {
            std::string alias = GetClassAlias(type);
            RegisterProperty(alias, name, value);
        }
    }

    inline std::string GetClassAlias(const std::type_info * type) {
        if (type) {
#ifdef HPROSE_THREAD_SAFE_REG
            boost::shared_lock<boost::shared_mutex> lock(mutex);
#endif
            return classAliases[type];
        } else {
            return "";
        }
    }

    inline const std::type_info * GetClass(const std::string & alias) {
        if (!alias.empty()) {
#ifdef HPROSE_THREAD_SAFE_REG
            boost::shared_lock<boost::shared_mutex> lock(mutex);
#endif
            Classes::const_iterator itr = classes.find(alias);
            return (itr != classes.end()) ? itr->second : NULL;
        } else {
            return NULL;
        }
    }

    inline ClassProperty * GetClassProperty(const std::string & alias) {
        if (!alias.empty()) {
#ifdef HPROSE_THREAD_SAFE_REG
            boost::shared_lock<boost::shared_mutex> lock(propertyMutex);
#endif
            ClassProperties::iterator itr = properties.find(alias);
            return (itr != properties.end()) ? &itr->second : NULL;
        } else {
            return NULL;
        }
    }

    inline ClassProperty * GetClassProperty(const std::type_info * type) {
        if (type) {
            std::string alias = GetClassAlias(type);
            return GetClassProperty(alias);
        } else {
            return NULL;
        }
    }

private:

    typedef std::tr1::unordered_map<const std::type_info *, std::string> ClassAliases;
    typedef std::tr1::unordered_map<std::string, const std::type_info *> Classes;
    typedef std::tr1::unordered_map<std::string, ClassProperty> ClassProperties;

private:

    ClassAliases classAliases;
    Classes classes;
    ClassProperties properties;

#ifdef HPROSE_THREAD_SAFE_REG
    boost::shared_mutex mutex; // read-write lock for classAliases and classes;
    boost::shared_mutex propertyMutex; // read-write lock for properties;
#endif

}; // class ClassManager

} // namespace hprose

#endif // HPROSE_IO_CLASS_MANAGER_HPP

ext: hpp

max_stars_repo_name: hprose/hprose-cpp

max_issues_repo_name: hprose/hprose-cpp

max_forks_repo_name: hprose/hprose-cpp


text:
/**
 * Code to compute CNN (ImageNet) features for a given image using CAFFE
 * (c) Rohit Girdhar
 */

#include <memory>
#include <chrono>
#include <opencv2/opencv.hpp>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp> // for to_lower
#include <hdf5.h>
#include <zmq.hpp>
#include "caffe/caffe.hpp"
#include "utils.hpp"
//#include "external/DiskVector/DiskVector.hpp"
#include "external/DiskVector/DiskVectorLMDB.hpp"
#include "lock.hpp"

using namespace std;
using namespace std::chrono;
using namespace caffe;
using namespace cv;
using namespace CNNFeatureUtils;
namespace po = boost::program_options;
namespace fs = boost::filesystem;

#define MAXFEATPERIMG 10000
#define PRINT_INTERVAL 20
// output type
#define OUTTYPE_TEXT 1
#define OUTTYPE_LMDB 2
#define OUTTYPE_HDF5 3

void dumpFeatures_txt(const fs::path&, const vector<vector<float>>&);
void dumpFeatures_hdf5(const fs::path&, vector<vector<float>>&);
long long hashCompleteName(long long, int);
void readImageUsingService(const string&, Mat&);
void readSegUsingService(const Mat&, Mat&);

int
main(int argc, char *argv[]) {
  ::google::InitGoogleLogging(argv[0]);
#ifdef CPU_ONLY
  Caffe::set_mode(Caffe::CPU);
  LOG(INFO) << "Extracting Features in CPU mode";
#else
  Caffe::set_mode(Caffe::GPU);
#endif

  po::options_description desc("Allowed options");
  desc.add_options()
    ("help,h", "Show this help")
    ("network-path,n", po::value<string>()->required(),
     "Path to the prototxt file")
    ("model-path,m", po::value<string>()->required(),
     "Path to corresponding caffemodel")
    ("outdir,o", po::value<string>()->default_value("output"),
     "Output directory")
    ("layer,l", po::value<string>()->default_value("pool5"),
     "CNN layer to extract features from")
    ("imgsdir,i", po::value<string>()->required(),
     "Input directory of images")
    ("imgslst,q", po::value<string>()->required(),
     "List of images relative to input directory")
    ("windir,w", po::value<string>()->default_value(""),
     "Input directory of all windows in each image (selective search format: y1 x1 y2 x2)."
     "Defaults to full image features."
     "Ignores sliding window if set.")
    ("sliding,s", po::value<bool>()->default_value(0),
     "Compute features in sliding window fashion")
    ("pool,p", po::value<string>()->default_value(""),
     "Pool the features from different regions into one feature."
     "Supports: <empty>: no pooling. store all features."
     "avg: avg pooling")
    ("segdir,x", po::value<string>()->default_value(""),
     "Directory with images with same filename as in the corpus images dir "
     "but uses it to prune the set of windows. "
     "By default keeps only those overlapping <0.2 with FG")
    ("seglist", po::value<string>()->default_value(""),
     "List with the paths (wrt segdir) for segmentations for each image. "
     "By default it uses from imgslst.")
    ("startimgid,z", po::value<long long>()->default_value(1),
     "The image id of the first image in the list."
     "Useful for testing parts of dataset because the selsearch boxes"
     "etc use the image ids. Give 1 indexed")
    ("output-type,t", po::value<string>()->default_value("lmdb"),
     "Output format [txt/lmdb/hdf5]")
    ("normalize,y", po::value<bool>()->default_value(0),
     "Enable feature L2 normalization")
    ("ids2compute4", po::value<string>()->default_value(""),
     "File with list of image ids (1 indexed) to compute the features"
     "for. If not specified, computes from start img idx to end")
    ("uniquelist", po::value<string>()->default_value(""),
     "File with unique/duplicate information using SHA1")
    ("foreground,f", po::value<bool>()->default_value(0),
     "Extract the features of the foreground")
    ;

  po::variables_map vm;
  po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
  if (vm.count("help")) {
    LOG(INFO) << desc;
    return -1;
  }
  try {
    po::notify(vm);
  } catch(po::error& e) {
    LOG(ERROR) << e.what();
    return -1;
  }

  fs::path NETWORK_PATH = fs::path(vm["network-path"].as<string>());
  fs::path MODEL_PATH =
    fs::path(vm["model-path"].as<string>());
  string LAYERS = vm["layer"].as<string>();
  vector<string> layers;
  boost::split(layers, LAYERS, boost::is_any_of(","));
  fs::path OUTDIR = fs::path(vm["outdir"].as<string>());
  fs::path IMGSDIR = fs::path(vm["imgsdir"].as<string>());
  fs::path IMGSLST = fs::path(vm["imgslst"].as<string>());
  fs::path WINDIR = fs::path(vm["windir"].as<string>());
  fs::path SEGDIR = fs::path(vm["segdir"].as<string>());
  fs::path SEGLIST = fs::path(vm["seglist"].as<string>());
  string POOLTYPE = vm["pool"].as<string>();
  bool NORMALIZE = vm["normalize"].as<bool>();
  bool FOREGROUND = vm["foreground"].as<bool>();
  cout << "FOREGROUND: " << FOREGROUND << endl;
  cout << "NORMALIZE: " << NORMALIZE << endl;
  long long START_IMGID = vm["startimgid"].as<long long>();
  int OUTTYPE = -1;
  if (vm["output-type"].as<string>().compare("text") == 0) {
    OUTTYPE = OUTTYPE_TEXT;
  } else if (vm["output-type"].as<string>().compare("lmdb") == 0) {
    OUTTYPE = OUTTYPE_LMDB;
  } else if (vm["output-type"].as<string>().compare("hdf5") == 0) {
    OUTTYPE = OUTTYPE_HDF5;
  } else {
    LOG(FATAL) << "Unknown output-type " << vm["output-type"].as<string>();
  }

  if (POOLTYPE.length() > 0) {
    LOG(INFO) << "Will be pooling with " << POOLTYPE;
  }

  Net<float> caffe_test_net(NETWORK_PATH.string(), caffe::TEST);
  caffe_test_net.CopyTrainedLayersFrom(MODEL_PATH.string());
  int BATCH_SIZE = caffe_test_net.blob_by_name("data")->num();

  // Get list of images in directory
  vector<fs::path> imgs;
  readList_withSpaces<fs::path>(IMGSLST, imgs);

  cout << "imgs  " << imgs.size() << endl;

  vector<fs::path> segpaths;
  if (SEGDIR.string().length() > 0 && (fs::exists(SEGDIR) ||
        SEGDIR.string().compare("service") == 0)) {
    LOG(INFO) << "Will be pruning the bounding boxes using "
              << "segmentation information";
    if (SEGLIST.string().length() > 0) {
      readList<fs::path>(SEGLIST, segpaths);
    } else {
      segpaths = imgs;
    }
  } else {
    SEGDIR = fs::path(""); // so that I don't need to check existance again
  }

  cout << "segpaths  " << segpaths.size() << endl;

  // Get list of image ids (1 indexed) to compute for
  vector<long long> ids2compute4;
  if (vm["ids2compute4"].as<string>().length() > 0) {
    readList(vm["ids2compute4"].as<string>(), ids2compute4);
  } else if (vm["uniquelist"].as<string>().length() > 0) {
    getUniqueIds(fs::path(vm["uniquelist"].as<string>()), ids2compute4, START_IMGID);
  } else {
    for (long long imgid = START_IMGID; imgid <= imgs.size(); imgid++) {
      ids2compute4.push_back(imgid);
    }
  }

  std::shared_ptr<DiskVectorLMDB<vector<float>>> dv;
  if (OUTTYPE == OUTTYPE_LMDB) {
    dv = std::shared_ptr<DiskVectorLMDB<vector<float>>>(
        new DiskVectorLMDB<vector<float>>(OUTDIR));
    // If it gets stuck here, it might be because the lock file is not
    // letting it update an existing store (maybe a unnatural exit last time).
    // Just delete the lock file (of course, if no other program is using it)
  }
  high_resolution_clock::time_point begin = high_resolution_clock::now();
  for (long long meta_i = 0; meta_i < ids2compute4.size(); meta_i++) {
    long long imgid = ids2compute4[meta_i];
    high_resolution_clock::time_point start = high_resolution_clock::now();
    fs::path imgpath = imgs[imgid - 1];
    cout << meta_i << " " << imgpath << endl;
    //cout << endl;
    //cout << "DEBUG: check100 " << meta_i << " " << ids2compute4.size() << endl;
    //cout << endl;
    if (meta_i % PRINT_INTERVAL == 0) {
      cout << "Doing for " << imgpath << " (" << meta_i << "/"
           << ids2compute4.size() << ")...";
    }

    vector<Mat> Is;
    Mat I;
    if (IMGSDIR.compare("service") == 0) {
      // read the image from the hbase based image read service
      readImageUsingService(imgpath.string(), I);
    } else {
      I = imread((IMGSDIR / imgpath).string());
    }
    Mat S; // get the segmentation image as well, used in debugging
    if (!I.data) {
      LOG(ERROR) << "Unable to read " << imgpath;
      continue;
    }
    vector<Rect> bboxes;
    bboxes.clear();
    if (WINDIR.string().size() > 0) {
      readBBoxesSelSearch<float>((WINDIR / (to_string((long long)imgid) + ".txt")).string(), bboxes);
    } else if (vm["sliding"].as<bool>()) {
      //  cout << "Debug: if 2 " << bboxes.size() << " " << vm["sliding"].as<bool>() << endl;
      genSlidingWindows(I.size(), bboxes);
    } else {
    //  cout << "Debug: if 1 " << bboxes.size() << endl;
      bboxes.push_back(Rect(0, 0, I.cols, I.rows)); // full image
    }
   // cout <<"DEBUG: bbox " << bboxes.size() << endl;
   // cout << endl;
    //cout << "DEBUG: check200 " << meta_i << endl;
    //cout << endl;
    // check if segdir defined. If so, then prune the list of bboxes
    if (SEGDIR.string().length() > 0) {
      if (SEGDIR.string().compare("service") == 0) {
        Mat S;
        readSegUsingService(I, S);
        cout << "DEBUG: check210 " << meta_i << endl;
        pruneBboxesWithSeg(I.size(), S, bboxes, FOREGROUND);
      } else {
        //cout << imgid << endl;
        //cout << segpaths.size() << endl;
        fs::path segpath = SEGDIR / segpaths[imgid - 1];
        if (!fs::exists(segpath)) {
          LOG(ERROR) << "Segmentation information not found for " << segpath;
        } else {
          //cout << "DEBUG: check250 " << meta_i << endl;
          pruneBboxesWithSeg(I.size(), segpath, bboxes, S, FOREGROUND);
        }
      }
    }
   // cout << endl;
   // cout << "DEBUG: check300 " << meta_i << endl;
   // cout << endl;
    LOG(INFO) << "Computing over " << bboxes.size() << " subwindows";
    // push in all subwindows
    for (int i = 0; i < bboxes.size(); i++) {
      Mat Itemp  = I(bboxes[i]);
      resize(Itemp, Itemp, Size(256, 256)); // rest of the transformation will run
                                            // like mean subtraction etc
      Is.push_back(Itemp);
    }
    // Uncomment to see the windows selected
    // DEBUG_storeWindows(Is, fs::path("temp/") / imgpath, I, S);

    // [layer[image[feature]]]
    vector<vector<vector<float>>> output;
    /**
     * Separately computing features for either case of text/hdf5 and lmdb because
     * can using locking (and run parallel) for text output
     */
    if (OUTTYPE == OUTTYPE_TEXT || OUTTYPE == OUTTYPE_HDF5) {
      string fext = ".txt";
      if (OUTTYPE == OUTTYPE_HDF5) {
        fext = ".h5";
      }

      fs::path outFile = fs::change_extension(OUTDIR / imgpath, fext);
      if (output.size() > 1) {
        outFile = fs::change_extension(OUTDIR / fs::path(layers[0]) / imgpath, fext);
      }
      if (!lock(outFile)) {
        continue;
      }
      computeFeaturesPipeline(caffe_test_net, Is, layers,
          BATCH_SIZE, output, false, POOLTYPE, NORMALIZE);
      for (int l = 0; l < output.size(); l++) {
        fs::path thisoutFile = fs::change_extension(OUTDIR / imgpath, fext);
        if (output.size() > 1) {
          thisoutFile = fs::change_extension(OUTDIR / fs::path(layers[l]) / imgpath, fext);
        }
        fs::create_directories(thisoutFile.parent_path());
        if (OUTTYPE == OUTTYPE_TEXT) {
          dumpFeatures_txt(thisoutFile, output[l]);
        } else {
          dumpFeatures_hdf5(thisoutFile, output[l]);
        }
      }
      unlock(outFile);
    } else if (OUTTYPE == OUTTYPE_LMDB) {
      if (layers.size() > 1) {
        LOG(FATAL) << "Multiple layer output is not suppported with lmdb output.";
      }
      computeFeaturesPipeline(caffe_test_net, Is, layers,
          BATCH_SIZE, output, false, POOLTYPE, NORMALIZE);
      // output into a DiskVector
      for (int i = 0; i < output[0].size(); i++) {
        dv->Put(hashCompleteName(imgid, i), output[0][i]);
      }
    }
    if (meta_i % PRINT_INTERVAL == 0) {
      high_resolution_clock::time_point end = high_resolution_clock::now();
      cout << "Done in " << duration_cast<milliseconds>(end - start).count()
           << "ms" << endl
           << "Average taking "
           << duration_cast<milliseconds>(end - begin).count() * 1.0f /
              (meta_i + 1) << "ms" << endl;
    }
  }

  return 0;
}

inline void dumpFeatures_txt(const fs::path& fpath, const vector<vector<float>>& feats) {
  FILE* fout = fopen(fpath.string().c_str(), "w");
  for (int fi = 0; fi < feats.size(); fi++) {
    for (int i = 0; i < feats[fi].size(); i++) {
      if (feats[fi][i] == 0) {
        fprintf(fout, "0 ");
      } else {
        fprintf(fout, "%f ", feats[fi][i]);
      }
    }
    fprintf(fout, "\n");
  }
  fclose(fout);
}

inline void dumpFeatures_hdf5(const fs::path& fpath, vector<vector<float>>& feats) {
  if (feats.size() == 0) {
    cerr << "No items in the feature. Not writing the file." << endl;
    return;
  }
  int DIM1 = feats.size();
  int DIM2 = feats[0].size();
  float* feats_raw = new float[DIM1 * DIM2];
  for (int i = 0; i < feats.size(); i++) {
    for (int j = 0; j < feats[i].size(); j++) {
      feats_raw[i * DIM2 + j] = feats[i][j];
    }
  }
  hsize_t dimsf[2];
  dimsf[0] = DIM1; dimsf[1] = DIM2;
  hsize_t cdims[2];
  cdims[0] = 1; cdims[1] = DIM2;
  hid_t file = H5Fcreate(fpath.string().c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
  hid_t dataspace = H5Screate_simple(2, dimsf, NULL);
  hid_t dcpl = H5Pcreate (H5P_DATASET_CREATE);
  H5Pset_deflate(dcpl, 6);
  H5Pset_chunk(dcpl, 2, cdims);
  hid_t dataset = H5Dcreate(file, "feats", H5T_NATIVE_FLOAT, dataspace, H5P_DEFAULT, dcpl,
      H5P_DEFAULT);
  H5Dwrite(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, feats_raw);

  delete[] feats_raw;
  H5Sclose(dataspace);
  H5Dclose(dataset);
  H5Fclose(file);
}

inline long long hashCompleteName(long long imgid, int id) { // imgid is 1 indexed, id is 0 indexed
  // both will be 1 indexed <convention set Saturday 11 April 2015 01:57:20 AM GMT>
  return imgid * MAXFEATPERIMG + id + 1;
}

void readImageUsingService(const string& imid, Mat& I) {
  const int port_num = 5554;
  static bool initialized = false;
  static zmq::context_t *context = NULL;
  static zmq::socket_t *socket = NULL;
  if (! initialized) {
    context = new zmq::context_t(1);
    socket = new zmq::socket_t(*context, ZMQ_REQ);
    socket->connect(("tcp://localhost:" + to_string((long long)port_num)).c_str());
    initialized = true;
  }
  zmq::message_t request(imid.length());
  memcpy((void *) request.data(), imid.c_str(), imid.length());
  socket->send(request);

  zmq::message_t reply;
  socket->recv(&reply);
  char *reply_data = static_cast<char*>(reply.data());
  vector<char> reply_vector_str(reply_data, reply_data + reply.size());
  try {
    I = imdecode(reply_vector_str, CV_LOAD_IMAGE_COLOR);
  } catch (cv::Exception& e) {
    LOG(ERROR) << "Unable to read image at " << imid << ". Returning black image... ";
    I = Mat(100, 100, CV_8UC3, Scalar(0,0,0));
  }
}

void readSegUsingService(const Mat& I, Mat& S) {
  const int port_num = 5556;
  const string TEMP_SEG_DIR = "/tmp/segtemp/computeFeatures/";
  const string qimg_path = TEMP_SEG_DIR + "img.jpg";
  const string res_path = "/srv2/rgirdhar/Work/Code/0005_ObjSegment/scripts/service_scripts/temp-dir/result.jpg";

  static bool initialized = false;
  static zmq::context_t *context = NULL;
  static zmq::socket_t *socket = NULL;
  if (! initialized) {
    context = new zmq::context_t(1);
    socket = new zmq::socket_t(*context, ZMQ_REQ);
    socket->connect(("tcp://localhost:" + to_string((long long)port_num)).c_str());
    initialized = true;
  }
  imwrite(qimg_path.c_str(), I);
  zmq::message_t request(qimg_path.length());
  memcpy((void *) request.data(), qimg_path.c_str(), qimg_path.length());
  socket->send(request);

  zmq::message_t reply;
  socket->recv(&reply);
  S = imread(res_path, CV_LOAD_IMAGE_GRAYSCALE);
}

max_stars_repo_name: USCDataScience/cmu-fg-bg-similarity

max_issues_repo_name: USCDataScience/cmu-fg-bg-similarity

max_forks_repo_name: USCDataScience/cmu-fg-bg-similarity


text:
#include <boost/test/unit_test.hpp>
#include "Operators.hpp"
#include "Error.hpp"
#include "Value.hpp"

using namespace slim;
BOOST_AUTO_TEST_SUITE(TestOperators)

double to_d(ObjectPtr val)
{
    auto n = dynamic_cast<const Number*>(val.get());
    if (n) return n->get_value();
    BOOST_FAIL("Not a number");
    return 0;
}

BOOST_AUTO_TEST_CASE(equals)
{
    auto a = make_value(55.0);
    auto a2 = make_value(55.0);
    auto b = make_value(60.0);
    auto c = make_value("55");
    auto d = make_value("test");
    auto d2 = make_value("test");
    //Boolean
    BOOST_CHECK(op_eq(TRUE_VALUE.get(), TRUE_VALUE.get())->is_true());
    BOOST_CHECK(!op_eq(TRUE_VALUE.get(), FALSE_VALUE.get())->is_true());
    BOOST_CHECK(!op_eq(FALSE_VALUE.get(), TRUE_VALUE.get())->is_true());

    BOOST_CHECK(!op_ne(TRUE_VALUE.get(), TRUE_VALUE.get())->is_true());
    BOOST_CHECK(op_ne(TRUE_VALUE.get(), FALSE_VALUE.get())->is_true());
    BOOST_CHECK(op_ne(FALSE_VALUE.get(), TRUE_VALUE.get())->is_true());

    //Null
    BOOST_CHECK(op_eq(NIL_VALUE.get(), NIL_VALUE.get())->is_true());
    BOOST_CHECK(!op_ne(NIL_VALUE.get(), NIL_VALUE.get())->is_true());

    //Null and Boolean
    BOOST_CHECK(!op_eq(TRUE_VALUE.get(), NIL_VALUE.get())->is_true());
    BOOST_CHECK(!op_eq(FALSE_VALUE.get(), NIL_VALUE.get())->is_true());

    //Number
    BOOST_CHECK(op_eq(a.get(), a2.get())->is_true());
    BOOST_CHECK(!op_eq(a.get(), b.get())->is_true());
    BOOST_CHECK(!op_ne(a.get(), a2.get())->is_true());
    BOOST_CHECK(op_ne(a.get(), b.get())->is_true());

    //String
    BOOST_CHECK(op_eq(d.get(), d2.get())->is_true());
    BOOST_CHECK(!op_eq(c.get(), d.get())->is_true());
    BOOST_CHECK(!op_ne(a.get(), a2.get())->is_true());
    BOOST_CHECK(op_ne(c.get(), d.get())->is_true());

    //Number and String
    BOOST_CHECK(!op_eq(a.get(), c.get())->is_true());
    BOOST_CHECK(op_ne(a.get(), c.get())->is_true());
}

BOOST_AUTO_TEST_CASE(rel_cmp)
{
    auto a = make_value(55.0);
    auto b = make_value(60.0);
    auto c = make_value("55");
    auto d = make_value("test");
    auto e = make_value("test55");
    auto f = make_value("56");

    //Number
    BOOST_CHECK_EQUAL(0, to_d(op_cmp(a.get(), a.get())));
    BOOST_CHECK_EQUAL(-1, to_d(op_cmp(a.get(), b.get())));
    BOOST_CHECK_EQUAL(1, to_d(op_cmp(b.get(), a.get())));

    BOOST_CHECK(!op_lt(a.get(), a.get())->is_true());
    BOOST_CHECK(op_le(a.get(), a.get())->is_true());
    BOOST_CHECK(!op_gt(a.get(), a.get())->is_true());
    BOOST_CHECK(op_ge(a.get(), a.get())->is_true());

    BOOST_CHECK(op_lt(a.get(), b.get())->is_true());
    BOOST_CHECK(op_le(a.get(), b.get())->is_true());
    BOOST_CHECK(!op_gt(a.get(), b.get())->is_true());
    BOOST_CHECK(!op_ge(a.get(), b.get())->is_true());

    BOOST_CHECK(!op_lt(b.get(), a.get())->is_true());
    BOOST_CHECK(!op_le(b.get(), a.get())->is_true());
    BOOST_CHECK(op_gt(b.get(), a.get())->is_true());
    BOOST_CHECK(op_ge(b.get(), a.get())->is_true());

    //Boolean
    BOOST_CHECK(op_le(TRUE_VALUE.get(), TRUE_VALUE.get())->is_true());
    BOOST_CHECK(op_le(FALSE_VALUE.get(), TRUE_VALUE.get())->is_true());

    BOOST_CHECK(op_lt(FALSE_VALUE.get(), TRUE_VALUE.get())->is_true());
    BOOST_CHECK(!op_lt(TRUE_VALUE.get(), FALSE_VALUE.get())->is_true());

    BOOST_CHECK(op_gt(TRUE_VALUE.get(), FALSE_VALUE.get())->is_true());
    BOOST_CHECK(!op_gt(FALSE_VALUE.get(), TRUE_VALUE.get())->is_true());

    //Null, no-order
    BOOST_CHECK_THROW(op_lt(NIL_VALUE.get(), NIL_VALUE.get()), UnorderableTypeError);

    //Number and Boolean, different types
    BOOST_CHECK_THROW(op_le(FALSE_VALUE.get(), a.get()), UnorderableTypeError);

    //String
    BOOST_CHECK(op_le(c.get(), d.get())->is_true());
    BOOST_CHECK(op_le(c.get(), f.get())->is_true());
    BOOST_CHECK(op_le(d.get(), e.get())->is_true());
}

BOOST_AUTO_TEST_CASE(binary)
{
    auto a = make_value(2.0);
    auto b = make_value(5.0);
    auto c = make_value("55");
    auto d = make_value("test");

    //mul
    BOOST_CHECK_EQUAL(10.0, to_d(a->mul(b.get())));
    BOOST_CHECK_THROW(a->mul(c.get()), TypeError);
    BOOST_CHECK_THROW(c->mul(d.get()), NoMethodError);

    //pow
    BOOST_CHECK_EQUAL(32.0, to_d(a->pow(b.get())));
    BOOST_CHECK_THROW(a->pow(c.get()), TypeError);
    BOOST_CHECK_THROW(c->pow(d.get()), NoMethodError);

    //div
    BOOST_CHECK_EQUAL(2.0 / 5.0, to_d(a->div(b.get())));
    BOOST_CHECK_EQUAL(5.0 / 2.0, to_d(b->div(a.get())));
    BOOST_CHECK_THROW(a->div(c.get()), TypeError);
    BOOST_CHECK_THROW(c->div(d.get()), NoMethodError);

    //mod
    BOOST_CHECK_EQUAL(2.0, to_d(a->mod(b.get())));
    BOOST_CHECK_EQUAL(1.0, to_d(b->mod(a.get())));
    BOOST_CHECK_THROW(a->mod(c.get()), TypeError);
    BOOST_CHECK_THROW(c->mod(d.get()), NoMethodError);

    //add
    BOOST_CHECK_EQUAL(7.0, to_d(a->add(b.get())));
    BOOST_CHECK_EQUAL(7.0, to_d(b->add(a.get())));
    BOOST_CHECK_THROW(a->add(c.get()), TypeError);
    BOOST_CHECK_EQUAL("55test", c->add(d.get())->to_string());
    BOOST_CHECK_EQUAL("test55", d->add(c.get())->to_string());

    //sub
    BOOST_CHECK_EQUAL(-3.0, to_d(a->sub(b.get())));
    BOOST_CHECK_EQUAL(3.0, to_d(b->sub(a.get())));
    BOOST_CHECK_THROW(a->sub(c.get()), TypeError);
    BOOST_CHECK_THROW(c->sub(d.get()), NoMethodError);

    //lshift
    BOOST_CHECK_EQUAL(2 << 5, to_d(a->bit_lshift(b.get())));
    BOOST_CHECK_THROW(a->bit_lshift(c.get()), TypeError);
    BOOST_CHECK_THROW(c->bit_lshift(d.get()), NoMethodError);

    //rshift
    BOOST_CHECK_EQUAL(2 >> 5, to_d(a->bit_rshift(b.get())));
    BOOST_CHECK_THROW(a->bit_rshift(c.get()), TypeError);
    BOOST_CHECK_THROW(c->bit_rshift(d.get()), NoMethodError);

    //and
    BOOST_CHECK_EQUAL(2 & 5, to_d(a->bit_and(b.get())));
    BOOST_CHECK_THROW(a->bit_and(c.get()), TypeError);
    BOOST_CHECK_THROW(c->bit_and(d.get()), NoMethodError);

    //or
    BOOST_CHECK_EQUAL(2 | 5, to_d(a->bit_or(b.get())));
    BOOST_CHECK_THROW(a->bit_or(c.get()), TypeError);
    BOOST_CHECK_THROW(c->bit_or(d.get()), NoMethodError);

    //xor
    BOOST_CHECK_EQUAL(2 ^ 5, to_d(a->bit_xor(b.get())));
    BOOST_CHECK_THROW(a->bit_xor(c.get()), TypeError);
    BOOST_CHECK_THROW(c->bit_xor(d.get()), NoMethodError);
}
BOOST_AUTO_TEST_CASE(unary)
{
    auto a = make_value(2.0);
    auto b = make_value(0.0);
    auto c = make_value("55");
    auto d = make_value("");

    //bit not
    BOOST_CHECK_EQUAL(double(~(int)2), to_d(a->bit_not()));
    BOOST_CHECK_THROW(c->bit_not(), NoMethodError);

    //not
    BOOST_CHECK(!op_not(a.get())->is_true());
    BOOST_CHECK(!op_not(b.get())->is_true());
    BOOST_CHECK(!op_not(c.get())->is_true());
    BOOST_CHECK(!op_not(d.get())->is_true());
    BOOST_CHECK(!op_not(TRUE_VALUE.get())->is_true());
    BOOST_CHECK(op_not(FALSE_VALUE.get())->is_true());

    //negative
    BOOST_CHECK_EQUAL(-2.0, to_d(a->negate()));
    BOOST_CHECK_THROW(c->negate(), NoMethodError);
    BOOST_CHECK_THROW(TRUE_VALUE->negate(), NoMethodError);
    BOOST_CHECK_THROW(NIL_VALUE->negate(), NoMethodError);
}
BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: wnewbery/cpp-slim

max_issues_repo_name: wnewbery/cpp-slim

max_forks_repo_name: wnewbery/cpp-slim


text:
// Copyright (C) 2010-2015 Internet Systems Consortium, Inc. ("ISC")
//
// 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 <config.h>

#include <stdint.h>

#include <cassert>

#include <boost/lexical_cast.hpp>

#include <exceptions/exceptions.h>

#include <dns/edns.h>
#include <dns/exceptions.h>
#include <dns/message.h>
#include <dns/messagerenderer.h>
#include <dns/name.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rrclass.h>
#include <dns/rrttl.h>
#include <dns/rrtype.h>

using namespace std;
using boost::lexical_cast;
using namespace isc::dns::rdata;
using namespace isc::util;

namespace isc {
namespace dns {

namespace {
// This diagram shows the wire-format representation of the TTL field of
// OPT RR and its relationship with implementation specific parameters.
//
//           0             7               15                              31
//          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//          | EXTENDED-RCODE|    VERSION     |D|               Z             |
//          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//                                          <= VERSION_SHIFT (16 bits)
//                          <= EXTRCODE_SHIFT (24 bits)
//EXTFLAG_DO:0 0 0 ....................... 0 1 0 0 0 0.....................0
//VER_MASK:  0 0 0 ........0 1 1 1 1 1 1 1 1 0 0 ..........................0

const unsigned int VERSION_SHIFT = 16;
const unsigned int EXTRCODE_SHIFT = 24;
const uint32_t VERSION_MASK = 0x00ff0000;
const uint32_t EXTFLAG_DO = 0x00008000;
}

EDNS::EDNS(const uint8_t version) :
    version_(version),
    udp_size_(Message::DEFAULT_MAX_UDPSIZE),
    dnssec_aware_(false)
{
    if (version_ > SUPPORTED_VERSION) {
        isc_throw(isc::InvalidParameter,
                  "failed to construct EDNS: unsupported version: " <<
                  static_cast<unsigned int>(version_));
    }
}

EDNS::EDNS(const Name& name, const RRClass& rrclass, const RRType& rrtype,
           const RRTTL& ttl, const Rdata&) :
    version_((ttl.getValue() & VERSION_MASK) >> VERSION_SHIFT)
{
    if (rrtype != RRType::OPT()) {
        isc_throw(isc::InvalidParameter,
                  "EDNS is being created with incompatible RR type: "
                  << rrtype);
    }

    if (version_ > EDNS::SUPPORTED_VERSION) {
        isc_throw(DNSMessageBADVERS, "unsupported EDNS version: " <<
                  static_cast<unsigned int>(version_));
    }

    if (name != Name::ROOT_NAME()) {
        isc_throw(DNSMessageFORMERR, "invalid owner name for EDNS OPT RR: " <<
                  name);
    }

    dnssec_aware_ = ((ttl.getValue() & EXTFLAG_DO) != 0);
    udp_size_ = rrclass.getCode();
}

string
EDNS::toText() const {
    string ret = "; EDNS: version: ";

    ret += lexical_cast<string>(static_cast<int>(getVersion()));
    ret += ", flags:";
    if (getDNSSECAwareness()) {
        ret += " do";
    }
    ret += "; udp: " + lexical_cast<string>(getUDPSize()) + "\n";

    return (ret);
}

namespace {
/// Helper function to define unified implementation for the public versions
/// of toWire().
template <typename Output>
int
toWireCommon(Output& output, const uint8_t version,
             const uint16_t udp_size, const bool dnssec_aware,
             const uint8_t extended_rcode)
{
    // Render EDNS OPT RR
    uint32_t extrcode_flags = extended_rcode << EXTRCODE_SHIFT;
    extrcode_flags |= (version << VERSION_SHIFT) & VERSION_MASK;
    if (dnssec_aware) {
        extrcode_flags |= EXTFLAG_DO;
    }

    // Construct an RRset corresponding to the EDNS.
    // We don't support any options for now, so the OPT RR can be empty.
    RRsetPtr edns_rrset(new RRset(Name::ROOT_NAME(), RRClass(udp_size),
                                  RRType::OPT(), RRTTL(extrcode_flags)));
    edns_rrset->addRdata(ConstRdataPtr(new generic::OPT()));

    edns_rrset->toWire(output);

    return (1);
}
}

unsigned int
EDNS::toWire(AbstractMessageRenderer& renderer,
             const uint8_t extended_rcode) const
{
    // If adding the OPT RR would exceed the size limit, don't do it.
    // 11 = len(".") + type(2byte) + class(2byte) + TTL(4byte) + RDLEN(2byte)
    // (RDATA is empty in this simple implementation)
    if (renderer.getLength() + 11 > renderer.getLengthLimit()) {
        return (0);
    }

    return (toWireCommon(renderer, version_, udp_size_, dnssec_aware_,
                         extended_rcode));
}

unsigned int
EDNS::toWire(isc::util::OutputBuffer& buffer,
             const uint8_t extended_rcode) const
{
    return (toWireCommon(buffer, version_, udp_size_, dnssec_aware_,
                         extended_rcode));
}

EDNS*
createEDNSFromRR(const Name& name, const RRClass& rrclass,
                 const RRType& rrtype, const RRTTL& ttl,
                 const Rdata& rdata,
                 uint8_t& extended_rcode)
{
    // Create a new EDNS object first for exception guarantee.
    EDNS* edns = new EDNS(name, rrclass, rrtype, ttl, rdata);

    // At this point we can update extended_rcode safely.
    extended_rcode = ttl.getValue() >> EXTRCODE_SHIFT;

    return (edns);
}

ostream&
operator<<(std::ostream& os, const EDNS& edns) {
    os << edns.toText();
    return (os);
}

} // end of namespace dns
} // end of namespace isc

ext: cc

max_stars_repo_name: telekom/dt-kea-netconf

max_issues_repo_name: telekom/dt-kea-netconf

max_forks_repo_name: telekom/dt-kea-netconf


text:
#include <functional>
#include <boost/foreach.hpp>

#include <framework/framework.h>
#include <framework/camera.h>
#include <framework/graphics.h>
#include <framework/gui/builder.h>
#include <framework/gui/button.h>
#include <framework/gui/checkbox.h>
#include <framework/gui/gui.h>
#include <framework/gui/widget.h>
#include <framework/gui/window.h>
#include <framework/input.h>
#include <framework/model.h>
#include <framework/model_manager.h>
#include <framework/scenegraph.h>
#include <framework/shader.h>
#include <framework/paths.h>
#include <framework/path_find.h>

#include <game/editor/tools/pathing_tool.h>
#include <game/editor/windows/main_menu.h>
#include <game/editor/editor_terrain.h>
#include <game/editor/editor_world.h>
#include <game/world/terrain_helper.h>

static int PATCH_SIZE = 32; // our patches are independent of the terrain patches

using namespace std::placeholders;
using namespace fw::gui;

//-----------------------------------------------------------------------------

enum IDS {
  START_ID,
  END_ID,
};

class pathing_tool_window {
private:
  window *_wnd;
  ed::pathing_tool *_tool;

  bool on_start_click(widget *w);
  bool on_end_click(widget *w);
  bool on_simplify_click(widget *w);

public:
  pathing_tool_window(ed::pathing_tool *tool);
  virtual ~pathing_tool_window();

  void show();
  void hide();
};

pathing_tool_window::pathing_tool_window(ed::pathing_tool *tool) :
    _tool(tool), _wnd(nullptr) {
  _wnd = builder<window>(px(10), px(30), px(100), px(94)) << window::background("frame")
      << (builder<button>(px(4), px(4), sum(pct(100), px(-8)), px(30)) << button::text("Start") << widget::id(START_ID)
          << widget::click(std::bind(&pathing_tool_window::on_start_click, this, _1)))
      << (builder<button>(px(4), px(38), sum(pct(100), px(-8)), px(30)) << button::text("End") << widget::id(END_ID)
          << widget::click(std::bind(&pathing_tool_window::on_end_click, this, _1)))
      << (builder<checkbox>(px(4), px(72), sum(pct(100), px(-8)), px(18)) << checkbox::text("Simplify")
          << widget::click(std::bind(&pathing_tool_window::on_simplify_click, this, _1)));
  fw::framework::get_instance()->get_gui()->attach_widget(_wnd);
}

pathing_tool_window::~pathing_tool_window() {
  fw::framework::get_instance()->get_gui()->detach_widget(_wnd);
}

void pathing_tool_window::show() {
  _wnd->set_visible(true);
}

void pathing_tool_window::hide() {
  _wnd->set_visible(false);
}

bool pathing_tool_window::on_start_click(widget *w) {
  ed::statusbar->set_message("Set test start...");
  _tool->set_test_start();
  _wnd->find<button>(START_ID)->set_pressed(true);
  _wnd->find<button>(END_ID)->set_pressed(false);
  return true;
}

bool pathing_tool_window::on_end_click(widget *w) {
  ed::statusbar->set_message("Set test end...");
  _tool->set_test_end();
  _wnd->find<button>(START_ID)->set_pressed(false);
  _wnd->find<button>(END_ID)->set_pressed(true);
  return true;
}

bool pathing_tool_window::on_simplify_click(widget *w) {
  _tool->set_simplify(dynamic_cast<checkbox *>(w)->is_checked());
  return true;
}

//-----------------------------------------------------------------------------

class collision_patch {
private:
  int _patch_x, _patch_z;
  static std::shared_ptr<fw::index_buffer> _ib;
  std::shared_ptr<fw::vertex_buffer> _vb;

public:
  void bake(std::vector<bool> &data, float *heights, int width, int length, int patch_x, int patch_z);

  void render(fw::sg::scenegraph &scenegraph, fw::matrix const &world);
};

std::shared_ptr<fw::index_buffer> collision_patch::_ib;
std::shared_ptr<fw::vertex_buffer> current_path_vb;
std::shared_ptr<fw::index_buffer> current_path_ib;

void collision_patch::bake(std::vector<bool> &data, float *heights, int width, int length, int patch_x, int patch_z) {
  if (!_ib) {
    std::vector<uint16_t> indices;
    game::generate_terrain_indices_wireframe(indices, PATCH_SIZE);

    _ib = std::shared_ptr<fw::index_buffer>(new fw::index_buffer());
    _ib->set_data(indices.size(), &indices[0]);
  }

  std::vector<fw::vertex::xyz_c> vertices((PATCH_SIZE + 1) * (PATCH_SIZE + 1));
  for (int z = 0; z <= PATCH_SIZE; z++) {
    for (int x = 0; x <= PATCH_SIZE; x++) {
      int ix = fw::constrain((patch_x * PATCH_SIZE) + x, width);
      int iz = fw::constrain((patch_z * PATCH_SIZE) + z, length);

      bool passable = data[(iz * width) + ix];
      fw::colour colour(passable ? fw::colour(0.1f, 1.0f, 0.1f) : fw::colour(1.0f, 0.1f, 0.1f));

      int index = z * (PATCH_SIZE + 1) + x;
      vertices[index] = fw::vertex::xyz_c(x, heights[iz * width + ix] + 0.1f, z, colour);
    }
  }

  _vb = fw::vertex_buffer::create<fw::vertex::xyz_c>();
  _vb->set_data(vertices.size(), vertices.data());
}

void collision_patch::render(fw::sg::scenegraph &scenegraph, fw::matrix const &world) {
  std::shared_ptr<fw::sg::node> node(new fw::sg::node());
  node->set_world_matrix(world);

  // we have to set up the scenegraph node with these manually
  node->set_vertex_buffer(_vb);
  node->set_index_buffer(_ib);
  node->set_primitive_type(fw::sg::primitive_linelist);
  std::shared_ptr<fw::shader> shader = fw::shader::create("basic.shader");
  std::shared_ptr<fw::shader_parameters> shader_params = shader->create_parameters();
  shader_params->set_program_name("notexture");
  node->set_shader(shader);
  node->set_shader_parameters(shader_params);

  scenegraph.add_node(node);
}

//-----------------------------------------------------------------------------

namespace ed {
REGISTER_TOOL("pathing", pathing_tool);

pathing_tool::pathing_tool(editor_world *wrld) :
    tool(wrld), _start_set(false), _end_set(false), _test_mode(test_none), _simplify(true) {
  _wnd = new pathing_tool_window(this);
  _marker = fw::framework::get_instance()->get_model_manager()->get_model("marker");
}

pathing_tool::~pathing_tool() {
  delete _wnd;
}

void pathing_tool::activate() {
  tool::activate();
  _wnd->show();

  int width = get_terrain()->get_width();
  int length = get_terrain()->get_length();
  _collision_data.resize(width * length);
  get_terrain()->build_collision_data(_collision_data);

  _patches.resize((width / PATCH_SIZE) * (length / PATCH_SIZE));

  std::shared_ptr<fw::timed_path_find> pf(
      new fw::timed_path_find(get_terrain()->get_width(), get_terrain()->get_length(), _collision_data));
  _path_find = pf;

  fw::input *inp = fw::framework::get_instance()->get_input();
  _keybind_tokens.push_back(
      inp->bind_key("Left-Mouse", fw::input_binding(std::bind(&pathing_tool::on_key, this, _1, _2))));
}

void pathing_tool::deactivate() {
  tool::deactivate();
  _wnd->hide();
}

int get_patch_index(int patch_x, int patch_z, int patches_width, int patches_length, int *new_patch_x,
    int *new_patch_z) {
  patch_x = fw::constrain(patch_x, patches_width);
  patch_z = fw::constrain(patch_z, patches_length);

  if (new_patch_x != 0)
    *new_patch_x = patch_x;
  if (new_patch_z != 0)
    *new_patch_z = patch_z;

  return patch_z * patches_width + patch_x;
}

void pathing_tool::render(fw::sg::scenegraph &scenegraph) {
  // we want to render the patches centred on where the camera is looking
  fw::camera *camera = fw::framework::get_instance()->get_camera();
  fw::vector cam_loc = camera->get_position();
  fw::vector cam_dir = camera->get_direction();
  fw::vector location = get_terrain()->get_cursor_location(cam_loc, cam_dir);

  int centre_patch_x = (int) (location[0] / PATCH_SIZE);
  int centre_patch_z = (int) (location[2] / PATCH_SIZE);

  int patch_width = get_terrain()->get_width() / PATCH_SIZE;
  int patch_length = get_terrain()->get_length() / PATCH_SIZE;
  for (int patch_z = centre_patch_z - 1; patch_z <= centre_patch_z + 1; patch_z++) {
    for (int patch_x = centre_patch_x - 1; patch_x <= centre_patch_x + 1; patch_x++) {
      int new_patch_x, new_patch_z;
      int patch_index = get_patch_index(patch_x, patch_z, patch_width, patch_length, &new_patch_x, &new_patch_z);

      if (!_patches[patch_index])
        _patches[patch_index] = bake_patch(new_patch_x, new_patch_z);

      fw::matrix world = fw::translation(static_cast<float>(patch_x * PATCH_SIZE), 0,
          static_cast<float>(patch_z * PATCH_SIZE));

      _patches[patch_index]->render(scenegraph, world);
    }
  }

  if (_start_set) {
    fw::matrix loc(fw::translation(_start_pos));
    _marker->set_colour(fw::colour(1, 0.1f, 1, 0.1f));
    _marker->render(scenegraph, loc);
  }

  if (_end_set) {
    fw::matrix loc(fw::translation(_end_pos));
    _marker->set_colour(fw::colour(1, 1, 0.1f, 0.1f));
    _marker->render(scenegraph, loc);
  }

  if (current_path_vb) {
    std::shared_ptr<fw::sg::node> sgnode(new fw::sg::node());
    sgnode->set_vertex_buffer(current_path_vb);
    sgnode->set_index_buffer(current_path_ib);
    sgnode->set_cast_shadows(false);
    sgnode->set_primitive_type(fw::sg::primitive_linelist);
    std::shared_ptr<fw::shader> shader = fw::shader::create("basic.shader");
    std::shared_ptr<fw::shader_parameters> shader_params = shader->create_parameters();
    shader_params->set_program_name("notexture");
    sgnode->set_shader(shader);
    sgnode->set_shader_parameters(shader_params);

    scenegraph.add_node(sgnode);
  }
}

std::shared_ptr<collision_patch> pathing_tool::bake_patch(int patch_x, int patch_z) {
  std::shared_ptr<collision_patch> patch(new collision_patch());
  patch->bake(_collision_data, get_terrain()->get_height_data(), get_terrain()->get_width(),
      get_terrain()->get_length(), patch_x, patch_z);
  return patch;
}

void pathing_tool::set_simplify(bool value) {
  _simplify = value;
  find_path();
}

void pathing_tool::set_test_start() {
  _test_mode = test_start;
}

void pathing_tool::set_test_end() {
  _test_mode = test_end;
}

void pathing_tool::stop_testing() {
  _test_mode = test_none;
}

void pathing_tool::on_key(std::string keyname, bool is_down) {
  if (_test_mode == test_none) {
    return;
  }

  if (!is_down) {
    if (_test_mode == test_start) {
      _start_pos = _terrain->get_cursor_location();
      _start_set = true;
    } else if (_test_mode == test_end) {
      _end_pos = _terrain->get_cursor_location();
      _end_set = true;
    }

    find_path();
  }
}

void pathing_tool::find_path() {
  if (!_start_set || !_end_set)
    return;

  std::vector<fw::vector> full_path;
  if (!_path_find->find(full_path, _start_pos, _end_pos)) {
    statusbar->set_message((boost::format("No path found after %1%ms") % (_path_find->total_time * 1000.0f)).str());
  } else {
    std::vector<fw::vector> path;
    _path_find->simplify_path(full_path, path);
    statusbar->set_message((boost::format("Path found in %1%ms, %2% nodes, %3% nodes (simplified)")
        % (_path_find->total_time * 1000.0f)
        % full_path.size()
        % path.size()).str());

    std::vector<fw::vertex::xyz_c> buffer;

    if (_simplify) {
      BOOST_FOREACH(fw::vector loc, path) {
        float height = get_terrain()->get_vertex_height(static_cast<int>(loc[0]), static_cast<int>(loc[2]));
        buffer.push_back(fw::vertex::xyz_c(loc[0], height + 0.2f, loc[2], fw::colour(1, 0.5f, 0.5f, 1)));
      }
    } else {
      BOOST_FOREACH(fw::vector loc, full_path) {
        float height = get_terrain()->get_vertex_height(static_cast<int>(loc[0]), static_cast<int>(loc[2]));
        buffer.push_back(fw::vertex::xyz_c(loc[0], height + 0.2f, loc[2], fw::colour(1, 0.5f, 0.5f, 1)));
      }
    }

    std::shared_ptr<fw::vertex_buffer> vb = fw::vertex_buffer::create<fw::vertex::xyz_c>();
    vb->set_data(buffer.size(), &buffer[0]);

    current_path_vb = vb;

    current_path_ib = std::shared_ptr<fw::index_buffer>(new fw::index_buffer());
    std::vector<uint16_t> indices(buffer.size());
    for (int i = 0; i < buffer.size(); i++) {
      indices[i] = i;
    }
    current_path_ib->set_data(indices.size(), indices.data());
  }
}

}

max_stars_repo_name: codeka/ravaged-planets

max_issues_repo_name: codeka/ravaged-planets

max_forks_repo_name: codeka/ravaged-planets


text:
/*! \file solveeom.cpp
    \brief 単振り子に対して運動方程式を解くクラスの実装

    Copyright © 2016-2018 @dc1394 All Rights Reserved.
    This software is released under the BSD 2-Clause License.
*/
#include "solveeom.h"
#include <cmath>                                // for std::sin, std::cos
#include <fstream>                              // for std::ofstream
#include <boost/assert.hpp>                     // for BOOST_ASSERT
#include <boost/format.hpp>                     // for boost::format
#include <boost/math/constants/constants.hpp>   // for boost::math::constants::pi
#include "solveeommain.h"

namespace solveeom {
    // #region コンストラクタ・デストラクタ

    SolveEoM::SolveEoM(float l, float r, float theta0) :
		Isconsider_Inertial_Resistance(nullptr, [this](auto isconsider_inertial_resistance) { return isconsider_inertial_resistance_ = isconsider_inertial_resistance; }),
        Theta([this] { return static_cast<float>(x_[0]); }, [this](auto theta) { return x_[0] = theta; }),
		Theta0(nullptr, [this](auto theta0) { return theta0_ = theta0; }),
		Time([this] { return static_cast<float>(t_); }, [this](auto t) { return t_ = t; }),
		V([this] { return static_cast<float>(l_ * x_[1]); }, [this](auto v) { return x_[1] = v / l_; }),
        l_(l),
		omega0_2_(g / l_),
        r_(r),
		m_(4.0 / 3.0 * boost::math::constants::pi<double>() * r * r * r * SolveEoM::ALUMINIUMRHO),
		gamma_(3.0 * boost::math::constants::pi<double>() * r_ * AIRMYU / m_),
		stepper_(SolveEoM::EPS, SolveEoM::EPS),
		t_(0.0),
		theta0_(theta0),
		x_({ theta0, 0.0 })
    {
    }

    // #endregion コンストラクタ・デストラクタ

    // #region publicメンバ関数

	float SolveEoM::gettheta_fumofumobun_approx() const
	{
		return static_cast<float>(theta0_ * std::exp(-gamma_ * t_) *
								  std::cos(std::sqrt((omega0_2_ - gamma_ * gamma_) * (3.0 + std::cos(theta0_ * std::exp(-gamma_ * t_)))) / 2.0 * t_));
	}

	float SolveEoM::getv_fumofumobun_approx() const
    {
		auto const term1 = -gamma_ * gettheta_fumofumobun_approx();

		auto const alpha = 0.5 * std::sqrt((omega0_2_ - gamma_ * gamma_) * (3.0 + std::cos(theta0_ * std::exp(-gamma_ * t_))));

		auto const term2 = -alpha * theta0_* std::exp(-gamma_ * t_) * std::sin(alpha * t_) *
						   (0.5 * theta0_ * gamma_ * std::exp(-gamma_ * t_) * std::sin(theta0_ * std::exp(-gamma_ * t_)) /
						   (3.0 + std::cos(theta0_ * std::exp(-gamma_ * t_))) * t_ + 1.0);

		return l_ * (term1 + term2);
    }

	float SolveEoM::kinetic_energy(double v) const
	{
		return static_cast<float>(0.5 * m_ * sqr(v));
	}

    float SolveEoM::operator()(float dt)
    {
        boost::numeric::odeint::integrate_adaptive(
            stepper_,
            getEoM(),
            x_,
            0.0,
            static_cast<double>(dt),
            SolveEoM::DX);

        return static_cast<float>(x_[0]);
    }

    void SolveEoM::operator()(double dt, std::string const & filename, double t)
    {
        std::ofstream result(filename);

        boost::numeric::odeint::integrate_const(
            stepper_,
            getEoM(),
            x_,
            0.0,
            t,
            dt,
            [&result, this](auto const & x, auto const t)
            {
				result << boost::format("%.3f, %.15f, %.15f\n") % t % x[0] % gettheta_fumofumobun_approx();
            });
    }

	float SolveEoM::potential_energy(double theta) const
	{
		return static_cast<float>(m_ * SolveEoM::g * l_ * (1.0 - std::cos(theta)));
	}

	void SolveEoM::timereset()
    {
		t_ = 0.0;
    }

    // #endregion publicメンバ関数

    // #region privateメンバ関数

	std::function<void(SolveEoM::state_type const &, SolveEoM::state_type &, double const)> SolveEoM::getEoM() const
    {
        auto const eom = [this](state_type const & x, state_type & dxdt, double const)
        {
            // dθ/dt = v / l
            dxdt[0] = x[1];

            // 振り子に働く力
            auto const f1 = -SolveEoM::g * std::sin(x[0]) / l_;

			// レイノルズ数
			auto const Re = 2.0 * r_ * std::fabs(l_ * x[1]) / AIRNYU;

            // 粘性抵抗
            auto const F = 6.0 * boost::math::constants::pi<double>() * AIRMYU * r_ * l_ * x[1];

			// レイノルズ数が閾値より小さいか、「慣性抵抗も考慮」チェックボックスが外れていたら
			if (Re < SolveEoM::REYNOLDS_THRESHOLD || !isconsider_inertial_resistance_) {
				// 粘性抵抗のみを考慮する
				dxdt[1] = f1 - F / (m_ * l_);

				return;
			}

			auto const FD = 0.5 * AIRRHO * boost::math::constants::pi<double>() * sqr(r_ * (l_ * x[1]));

			// Drag coefficient
			double CD;

			// N.-S. Cheng, Comparison of formulas for drag coefficient and settling velocity of
			// spherical particles, Powder Technology 189 (2009) 395–398.
			if (Re <= 3000) {
				CD = 24.0 / Re * std::pow(1.0 + 0.27 * Re, 0.43) + 0.47 * (1.0 - std::exp(-0.04 * std::pow(Re, 0.38)));
			}
			else {
				// Re > 3000
				// Almedeij J. Drag coefficient of flow around a sphere: Matching asymptotically the wide
				// trend. std::powder Technology. (2008);doi:10.1016/j.std::powtec.2007.12.006.
				auto const phi1 = std::pow(24.0 / Re, 10) + std::pow(21.0 * std::pow(Re, -0.67), 10) +
					std::pow(4.0 * std::pow(Re, -0.33), 10) + std::pow(0.4, 10);
				auto const phi2 = 1.0 / (1.0 / std::pow(0.148 * std::pow(Re, 0.11), 10) + 1.0 / std::pow(0.5, 10));
				auto const phi3 = std::pow((1.57E+8) * std::pow(Re, -1.625), 10);
				auto const phi4 = 1.0 / (1.0 / std::pow((6.0E-17) * std::pow(Re, 2.63), 10) + 1.0 / std::pow(0.2, 10));

				CD = std::pow((1.0 / (1.0 / (phi1 + phi2) + 1.0 / phi3) + phi4), 0.1);
			}

			// 慣性抵抗÷(m×l)
			auto const f2 = (x[1] >= 0.0) ? -FD * CD / (m_ * l_) : FD * CD / (m_ * l_);
			dxdt[1] = f1 + f2 - F / (m_ * l_);
        };

        return eom;
    }

    // #endregion privateメンバ関数
}


text:
#ifndef BART_SRC_DOMAIN_DOMAIN_TYPES_HPP_
#define BART_SRC_DOMAIN_DOMAIN_TYPES_HPP_

#include <deal.II/dofs/dof_accessor.h>

#include "utility/named_type.h"

namespace bart::domain {

template <int dim>
using CellPtr = typename dealii::DoFHandler<dim>::active_cell_iterator;

using FaceIndex = bart::utility::NamedType<int, struct FaceIndexParameter>;

} // namespace bart::domain

#endif // BART_SRC_DOMAIN_DOMAIN_TYPES_HPP_

max_stars_repo_name: SlaybaughLab/Transport

max_issues_repo_name: SlaybaughLab/Transport

max_forks_repo_name: SlaybaughLab/Transport


text:
//  (C) Copyright Matt Borland 2021.
//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <vector>
#include <boost/math/tools/random_vector.hpp>
#include <boost/math/statistics/bivariate_statistics.hpp>
#include <benchmark/benchmark.h>

using boost::math::generate_random_vector;

template<typename T>
void seq_covariance(benchmark::State& state)
{
    constexpr std::size_t seed {};
    const std::size_t size = state.range(0);
    std::vector<T> u = generate_random_vector<T>(size, seed);
    std::vector<T> v = generate_random_vector<T>(size, seed);

    for(auto _ : state)
    {
        benchmark::DoNotOptimize(boost::math::statistics::covariance(std::execution::seq, u, v));
    }
    state.SetComplexityN(state.range(0));
}

template<typename T>
void par_covariance(benchmark::State& state)
{
    constexpr std::size_t seed {};
    const std::size_t size = state.range(0);
    std::vector<T> u = generate_random_vector<T>(size, seed);
    std::vector<T> v = generate_random_vector<T>(size, seed);

    for(auto _ : state)
    {
        benchmark::DoNotOptimize(boost::math::statistics::covariance(std::execution::par, u, v));
    }
    state.SetComplexityN(state.range(0));
}

template<typename T>
void seq_correlation(benchmark::State& state)
{
    constexpr std::size_t seed {};
    const std::size_t size = state.range(0);
    std::vector<T> u = generate_random_vector<T>(size, seed);
    std::vector<T> v = generate_random_vector<T>(size, seed);

    for(auto _ : state)
    {
        benchmark::DoNotOptimize(boost::math::statistics::correlation_coefficient(std::execution::seq, u, v));
    }
    state.SetComplexityN(state.range(0));
}

template<typename T>
void par_correlation(benchmark::State& state)
{
    constexpr std::size_t seed {};
    const std::size_t size = state.range(0);
    std::vector<T> u = generate_random_vector<T>(size, seed);
    std::vector<T> v = generate_random_vector<T>(size, seed);

    for(auto _ : state)
    {
        benchmark::DoNotOptimize(boost::math::statistics::correlation_coefficient(std::execution::par, u, v));
    }
    state.SetComplexityN(state.range(0));
}

BENCHMARK_TEMPLATE(seq_covariance, double)->RangeMultiplier(2)->Range(1 << 6, 1 << 20)->Complexity(benchmark::oN)->UseRealTime();
BENCHMARK_TEMPLATE(par_covariance, double)->RangeMultiplier(2)->Range(1 << 6, 1 << 20)->Complexity(benchmark::oN)->UseRealTime();

BENCHMARK_TEMPLATE(seq_correlation, double)->RangeMultiplier(2)->Range(1 << 6, 1 << 20)->Complexity(benchmark::oN)->UseRealTime();
BENCHMARK_TEMPLATE(par_correlation, double)->RangeMultiplier(2)->Range(1 << 6, 1 << 20)->Complexity(benchmark::oN)->UseRealTime();

BENCHMARK_MAIN();

max_stars_repo_name: oleg-alexandrov/math

max_issues_repo_name: oleg-alexandrov/math


text:
/*
Copyright 2018 Sahaj Software Solutions, Inc.

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.
*/

#include <boost/python.hpp>
#include <Eigen/Dense>

using Eigen::MatrixXd;

double greet(int N)
{
  MatrixXd m = MatrixXd::Random(N,N);
  return m.norm();
}

BOOST_PYTHON_MODULE(hello)
{
    using namespace boost::python;
    def("greet", greet);
}

max_stars_repo_name: ysraell/examples

max_issues_repo_name: ysraell/examples

max_forks_repo_name: ysraell/examples