mardi 4 août 2015

Using SDL2 for software development

I was surfing the internet this morning and I came upon reading about SDL2 game programing library for c++. Looking at how SDL2 works, I wonder if actual software applications can be developed with game programing libraries like SDL2. Why or why not would be really helpful for me to understand the reasons why you would ever do this. Thanks!!



via Chebli Mohamed

How can a bitwise operator be used to read a file?

I'm trying to read from a .dat file using the following code:

ifstream input_file; 
double x;
while (input_file >> x) {...}

I don't understand how this actually works though - input_file >> x seems like it's using the right bit-shift operator. In what way does that actually read the file?



via Chebli Mohamed

How to call a value template inside a type template [duplicate]

This question already has an answer here:

The idea is to provide a compile time integer element to a specific function of type S, for different types such as S.

G++ does not allow code below. Who can help me to fix this?

template <class T, int s>
void fn1(T &t)
{
    t.fn2<s>();
}

struct S
{
    template <int s> void fn2 ()
    {
    }
};

void test()
{
    S s;

    fn1<S,3>(s);
}

which leads to:

$ g++ -c t.cpp
t.cpp: In function ‘void fn1(T&)’:
t.cpp:4:11: error: expected primary-expression before ‘)’ token
  t.fn2<s>();
           ^
t.cpp: In instantiation of ‘void fn1(T&) [with T = S; int s = 3]’:
t.cpp:18:12:   required from here
t.cpp:4:7: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’
  t.fn2<s>();



via Chebli Mohamed

How to return the derived type from a template class

I want to be able to call makeAnother() this without providing a template argument. I've tried using different forms of decltype(this) both in and outside of the function body with no success.

#include <vector>

template <class T>
class A {
    template <class Derived>
    Derived makeAnother() {
        Derived result;
        /* do stuff */
        return result;
    }
    std::vector<T> v;
};

class B : A<int>
{
    /* stuff */
};

int main() {
    B foo;
    B result = foo.makeAnother(); // this doesn't work
}



via Chebli Mohamed

googletest SetUp Method not called

I'm using Google Test to unit test my C++ project. The getting started guide says:

If necessary, write a default constructor or SetUp() function to prepare the objects for each test. A common mistake is to spell SetUp() as Setup() with a small u - don't let that happen to you.

SetUp() is spelled correctly, but I still can't get SetUp to work. Any ideas?

#include "gtest/gtest.h"

class SampleTest : public testing::Test {
 protected:
  virtual void SetUp() { std::cout << "SetUp called." << std::endl; }
};

TEST(SampleTest, OneEqualsOne) {
  int one = 1;
  ASSERT_EQ(1, one);
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

g++ -g -Wno-deprecated -I gtest/include SampleTest.cpp gtest/libgtest.a -o SampleTest

Output:

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from SampleTest
[ RUN      ] SampleTest.OneEqualsOne
[       OK ] SampleTest.OneEqualsOne (1 ms)
[----------] 1 test from SampleTest (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 1 test.



via Chebli Mohamed

Bridging objective-c library in swift project does not work

In my swift project in need to use snmp++ project ( http://ift.tt/1fyN2Ff ). The snmp++ project is written in c++ and then objective-c wrapper is created for functions.

The project generates libMobileSNMP_PP.a file which i include in my swift project and then create a bridging header and in the bridging header inport "XISMobile_SNMP_PP.h".

Also included .mm and .h files in the swift project as shown in the attached image example1

enter image description here

at compile it gives "could not reference bridging file in the app".

I refered link Can I mix Swift with C++? Like the Objective - C .mm files but still issue exist.

I even tried steps as given in example as shown http://ift.tt/1P42pSz but no success.

Please tell where i'm doing or missing any step.



via Chebli Mohamed

OpenMP Single Producer Multiple Consumer

I am trying to achieve something contrived using OpenMP.

I have a multi-core system with N available processors. I want to have a vector of objects of length k*P to be populated in batches of P by a single thread (by reading a file), i.e. a single thread reads this file and writes in vecObj[0 to P-1] then vecObj[p to 2P-1] etc. To make things simple, this vector is pre-resized (i.e. inserting using = operator, no pushbacks, constant length as far as we are concerned).

After a batch is written into the vector, I want the remaining N-1 threads to work on the available data. Since every object can take different time to be worked upon, it would be good to have dynamic scheduling for the remaining threads. The below snippet works really well when all the threads are working on the data.

#pragma omp parallel for schedule(dynamic, per_thread)
    for(size_t i = 0; i < dataLength(); ++i) {
        threadWorkOnElement(vecObj, i);
    }

Now, according to me, the the main issue I am facing in thinking up of a solution is the question as to how can I have N-1 threads dynamically scheduled over the range of available data, while another thread just keeps on reading and populating the vector with data?

I am guessing that the issue of writing new data and messaging the remaining threads can be achieved using std atomic.

I think that what I am trying to achieve is along the lines of the following pseudo code

std::atomic<size_t> freshDataEnd;
size_t dataWorkStart = 0;
size_t dataWorkEnd;
#pragma omp parallel
{
    #pragma omp task
    {
        //increment freshDataEnd atomically upon reading every P objects
        //return when end of file is reached
        readData(vecObj, freshDataEnd);
    }
    #pragma omp task
    {
        omp_set_num_threads(N-1);           
        while(freshDataEnd <= MAX_VEC_LEN) {
            if (dataWorkStart < freshDataEnd) {
                dataWorkEnd = freshDataEnd;
                #pragma omp parallel for schedule(dynamic, per_thread)
                for(size_t i = dataWorkStart; i < dataWorkEnd; ++i) {
                    threadWorkOnElement(vecObj, i);
                }
                dataWorkStart = dataWorkEnd;
            }
        }
    }
}

Is this the correct approach to achieve what I am trying to do? How can I handle this sort of nested parallelism? Not so important : I would have preferred to stick with openmp directives and not use std atomics, is that possible? How?



via Chebli Mohamed