a find function for torch, for use in indexing multidimensional tensors

Summary:
implements a find function for torch: input a 1d
torch.ByteTensor, and outputs the nonzero entries.  for example

a=torch.randn(500,5)
bigid=find(a:norm(2,2):ge(1):squeeze());
c=a:index(1,bigid)

builds a tensor c with the rows of a that have norm bigger than 1.

Test Plan: spot checked in torch with various inputs

Reviewed By: kma@fb.com

Subscribers: tulloch, tudorb

FB internal diff: D1456226
DataParallel and its pre-reqs.

Summary:
Here's a basic implementation of DataParallel for Torch,
and all the scaffolding I needed to get it working. DataParallel is
mostly ModelParallel, but it distributes its inputs differently. I've
only tested it on a toy problem so far, but getting this in may make
it easier to parallelize other projects, like the Krizhevsky swizzling
idea.

Other significant pieces here:
- An adapter for the optim package that can work across GPUs.

- Getting ModelParallel mnist working, now that the optim package
can work on more than one GPU.

- A lot of debugger prints, some of which I want to remove, and some
of which I want to move to a nicer debug facility with more granular
environmental controls that is yet to be built.

- Checking the launch GPU matches the operands' GPU in lots of
low-level Tensor code, so we don't just get "Launch failed."

Test Plan: runtests, test_DataParallel, test_ModelParallel.

Reviewed By: tudorb@fb.com

Subscribers: tulloch

FB internal diff: D1456161

Tasks: 4371019
FFI fb.util.find

Summary:
Use the FFI raw access functions. No performance difference:

C function:

``` lang=text
>>>>>>>>> <TestFunction>
>>> <TestFunction>:testfind1d
>>> <TestFunction>:testfind2d

real    0m6.773s
user    0m6.533s
sys     0m0.216s
```

FFI:

``` lang=text
>>>>>>>>> <TestFunction>
>>> <TestFunction>:testfind1d
>>> <TestFunction>:testfind2d

real    0m6.815s
user    0m6.558s
sys     0m0.231s
```

Also, move find into fb.util.

Test Plan: find_test

Reviewed By: kma@fb.com

Subscribers: tulloch, jhj, lesha

FB internal diff: D1473191
Wrapper around std::string; c_escape / c_unescape

Summary:
I found myself needing to write C-escaped strings from Lua, and we already have
a function that does that in folly. This lead to std_string.

Test Plan: tests added

Reviewed By: tulloch@fb.com

Subscribers: lesha, jhj

FB internal diff: D1474463
Digraph library

Summary:
Pretty rudimentary for now, but it has basic operations and topological
sorting.

Test Plan: test added

Reviewed By: tulloch@fb.com

Subscribers: jhj, lesha

FB internal diff: D1488135
Reactor library wrapping EventBase

Summary:
Wrap EventBase to be used as the core of a Reactor pattern.

We couldn't write something in pure Lua as we need a way to schedule a callback
from C++ code running in another thread. Wrapping EventBase is useful as we
can integrate with Thrift async callbacks further down the road.

Test Plan: test added

Reviewed By: tulloch@fb.com

Subscribers: lesha, jhj, fjargsto

FB internal diff: D1488378
Async random number generation on the host

Summary:
Generate random numbers in different threads. All C++11 distributions are
supported. The numbers are not reproducible, as the per-thread generators
have different seeds and there's no way to control the interleaving.

This is similar in spirit and implementation to the async batch loader.

This gives about 9-10 ms / minibatch in imagenet, and it's also useful on
its own.

Test Plan: by hand, imagenet

Reviewed By: tulloch@fb.com

Subscribers: tulloch, ntv, fjargsto, jhj, lesha, spiantino

FB internal diff: D1502318
fbcode: __x__-protect all __attribute__ keywords, mechanically

Summary:
The first (and sometimes 2nd) argument to __attribute__
should begin and end with "__". If it does not, then it
is at risk of conflicting with user cpp-defined symbols.
Don't do that.  This change mechanically corrects most
such violations in fbcode.  A companion change (see parent task)
adds a lint rule to help keep things clean going forwards.

Define this function to filter the list of file names to which
we will apply the transformations. First, ensure we modify only
C and C++ sources.  The second process excludes a few directories
that appear to contain third-party code:

filter() {
grep -E '\.(c|h|cc|cpp|hpp)$' | grep -vE \
'^external/|/unbound/|/gearmand_|(3rd|third.)party|/external/libevent'
}

then, run this command,

git grep -l __attribute__ | filter \
| xargs perl -pi -e 's/(__attribute__ *)\(\(( *[^_]\w+)/$1((__${2}__/g'

inducing changes like this, for each such keyword:

-} __attribute__((packed));
+} __attribute__((__packed__));

That got all but the __format__ archetype arguments like this:
__attribute__((__format__(printf.  That final "printf"
keyword should have the "__" prefix and suffix, too.

Run this command to transform those remaining uses:

git grep -l '__attribute__.*__format' | filter | xargs perl -pi -e \
's/(__attribute__ *\(\(__format__ *)\( *(printf|scanf|strftime|strfmon)/$1(__${2}__/g'

That command induces changes like this:

-static inline int __attribute__ ((__format__ (printf, 3, 4)))
+static inline int __attribute__ ((__format__ (__printf__, 3, 4)))

This exercise is useful to avoid spurious name-space conflicts,
especially when the unprotected keyword is used in a header file.

Test Plan:
I've ensured tao builds and passes its tests.
I will watch the arc-run tests.

Reviewed By: njormrod@fb.com

Subscribers: rounak, trunkagent, hphp-diffs@, nli, ps, fbcode-common-diffs@, mcdonald, chaoyc, bill, search-fbcode-diffs@, lars, net-systems@, davejwatson, varunk, ruibalp, hero-diffs@, andrewcox, sorg, dfechete, dhruba, sdoroshenko, mcduff, marccelani, hitesh, mshneer, cold-storage-diffs@, omry, jcoens, unicorn-diffs@, ldbrandy, sumeet, abirchall, fugalh, atlas2-eng@, dcapra, mpawlowski, alandau, nkgupta, shilin, bmatheny, everstore-dev@, zhuohuang, wormhole-diffs@, vnalla, msk, maoy, mwa, jgehring, adsatlasreporting@, mconnor, oujin, bwester, micha, tulloch, ptc, logdevice-diffs, alikhtarov, shikong, fuegen

FB internal diff: D1508983

Tasks: 4947824
Atomic vector for refcounted contents.

Summary:
Something like this? Still lots of test code to write and cleanup
to do, but I think this is roughly sound. Turns out the over-refcounting I
described earlier is not needed; I think the SensitiveSection abstraction
suffices.

Test Plan: New tests in both C++ and Lua. C++ seems more able to suss out low-level races in practice, and Tudor has said he might want to use AtomicVector<T> for C++ anyway.

Reviewed By: tudorb@fb.com

Subscribers: lubomir, jase, tulloch

FB internal diff: D1480283

Tasks: 4819016
Add luaGetBoolean*

Summary: Mostly for completeness.

Test Plan: used it

Reviewed By: tulloch@fb.com

Subscribers: fjargsto, pamelavagata, jhj, ntv, lesha

FB internal diff: D1535384
Fix atomic_vector_test

Summary:
The "undefined symbol" errors are all magically fixed (they were likely broken
due to some of my LAR diffs last week) but atomic_vector_test had a genuine
failure.

Test Plan: ran it

Reviewed By: ntv@fb.com

Subscribers: ntv, fjargsto, pamelavagata, jhj, lesha

FB internal diff: D1552980
Add edge payload

Summary: Digraphs can have things attached to their edges.

Test Plan: tests added

Reviewed By: tulloch@fb.com

Subscribers: ntv, fjargsto, lesha, jhj

FB internal diff: D1543696
FBLuaLib changes: util.filter_keys(), Digraph:for_each(), Thrift serialization for Penlight classes

Summary: What it says above.

Test Plan: tests added

Reviewed By: tulloch@fb.com

Subscribers: fjargsto, ntv, jhj, lesha, pamelavagata

FB internal diff: D1551771
TH++ refcounting cleanup, remove & overload, extra fb.thrift debug logging

Summary:
There were a few bugs related to TH++ refcounting (summarized in a long comment
in one of the files, you'll see it in the diff). Cleaned up the code -- Storage
will only share memory with unique IOBufs (which then become non-unique;
isSharedOne() will return true); this requires more careful handling of
IOBuf lifetimes, so I removed the confusing non-&& overloads from the
deserializing constructors.

Test Plan: thpp/test, fblualib/*/test, others

Reviewed By: rfarnham@fb.com

Subscribers: pamelavagata, salex, fjargsto, ntv, jhj, lesha

FB internal diff: D1559018
LuaUnit: optionally abort on first failure; do not crash on C++ exception

Test Plan: used it

Reviewed By: tulloch@fb.com

Subscribers: fjargsto, ntv, jhj, lesha, pamelavagata

FB internal diff: D1559023
Make Digraph Thrift-serializable

Test Plan: used it

Reviewed By: tulloch@fb.com

Subscribers: fjargsto, ntv, jhj, lesha, pamelavagata

FB internal diff: D1559030
Wrap MPStrDict for Lua.

Summary:
MPStrDict is a CPU- and memory-efficient way of representing
potentially very large string<->integer bijections. Search back-ends
have been representing hundreds of millions of tokens with this structure
for a long time now.

The mapping is built online, and can be shared across threads that read and write
in an obstruction-free manner.

The thread library makes sharing a dictionary across threads hard in a
way analogous to AtomicVector, so reuse AtomicVector's named registry.

Test Plan: test program works; atomicvector_test still works.

Reviewed By: tulloch@fb.com

Subscribers: trunkagent, fbcode-common-diffs@, tulloch, aszlam, pamelavagata, ntv, sashar, jase, jhj

FB internal diff: D1578190

Tasks: 5233452
Fix ffivector_test.

Summary: I broke this for no good reason at all. Sorry.

Test Plan: fbconfig fblualib/ffivector/test && fbmake runtests

Reviewed By: pamelavagata@fb.com

FB internal diff: D1588036

Tasks: 5096599
Fix atomicVector C++ tests.

Summary:
We no longer allow writing one past the vector end
as a synonym for "append".

Test Plan: fbconfig fblualib/atomicvector && fbmake runtests

Reviewed By: tulloch@fb.com

FB internal diff: D1592018

Tasks: 5294333
Adding Torch functions to save to file and load from file atomic vectors

Summary:
Two little functions to save and load atomic vectors. I tried to insert this nicely in the existing code.
I had to overload fblualib/atomicvector/fb/atomicvector/init.lua because I did not find any other way to do this properly. But If there is, I would be happy to modify.

This seems to work btw. But any comments are more than welcomed.

Test Plan: I wrote a test that passes. All other tests still pass too.

Reviewed By: kma@fb.com

Subscribers: spchopra

FB internal diff: D1597206