Filter a large file quickly

11

2

The challenge is to filter a large file quickly.

  • Input: Each line has three space separated positive integers.
  • Output: All input lines A B, Tthat satisfy either of the following criterion.

    1. There exists another input line C, D, U where D = A and 0 <= T - U < 100.
    2. There exists another input line C, D, U where B = C and 0 <= U - T < 100.

To make a test file use the following python script which will also be used for testing. It will make a 1.3G file. You can of course reduce nolines for testing.

import random    
nolines = 50000000 # 50 million
for i in xrange(nolines):
    print random.randint(0,nolines-1), random.randint(0,nolines-1), random.randint(0,nolines-1)

Rules. The fastest code when tested on an input file I make using the above script on my computer wins. The deadline is one week from the time of the first correct entry.

My Machine The timings will be run on my machine. This is a standard 8GB RAM ubuntu install on an AMD FX-8350 Eight-Core Processor. This also means I need to be able to run your code.

Some relevant timing information

Timings updated to run the following before each test.

sync && sudo bash -c 'echo  3 > /proc/sys/vm/drop_caches'

time wc test.file

real    0m26.835s
user    0m18.363s
sys     0m0.495s

time sort -n largefile.file  > /dev/null

real    1m32.344s
user    2m9.530s
sys     0m6.543s

Status of entries

I run the following line before each test.

sync && sudo bash -c 'echo  3 > /proc/sys/vm/drop_caches'
  • Perl (Waiting for bug fix.)
  • Scala 1 minutes 37 seconds by @James_pic. (Using scala -J-Xmx6g Filterer largefile.file output.txt)
  • Java. 1 minute 23 seconds by @Geobits. (Using java -Xmx6g Filter_26643)
  • C. 2 minutes 21 seconds by @ScottLeadley.
  • C. 28 seconds by @James_pic.
  • Python+pandas. Maybe there is a simple "groupby" solution?
  • C. 28 seconds by @KeithRandall.

The winners are Keith Randall and James_pic.

I couldn't tell their running times apart and both are almost as fast as wc!

user9206

Posted 2014-05-05T20:12:10.653

Reputation:

1Maybe you should try writing a challenge that isn't [fastest-code]. – Justin – 2014-05-05T20:52:15.883

Why did you strip the user and sys time for wc? These are relevant because they show how much of the time elapsed was io and how much CPU (iow, was your cache primed when you ran your time wc invocation?) – skibrianski – 2014-05-05T22:45:05.627

@skibrianski Fixed. – None – 2014-05-06T08:18:27.663

2Please define positive integer. 1 < n < 2147483647? – durron597 – 2014-05-07T21:01:54.470

Oh, or reading your sample, the numbers are bounded by the number of lines in the file? – durron597 – 2014-05-07T21:04:18.113

Does the order of the output lines matter? Also, should the lines be output as a pair or can we just show the lines that match either condition in arbitrary order? – skibrianski – 2014-05-08T01:03:34.947

@skibrianski The order of the output lines is not important. The output lines should simply be a subset of the input lines so yes you should output the lines that match either condition in arbitrary order. – None – 2014-05-08T05:51:50.890

@durron597 Yes I will only test on files that are made using my sample python code. – None – 2014-05-08T05:52:20.517

If a line has multiple matches, can it be printed multiple times? (Yes would be good for parallelizing the problem and paralyzing your computer.) – Scott Leadley – 2014-05-09T06:55:17.617

1@ScottLeadley No, unless it appeared multiple times in the input of course (which I think is very unlikely). – None – 2014-05-09T07:32:33.827

Please define what a positive integer is. Also, what does "either" mean in your question. Does it mean at least one condition, or does it mean one condition, but not both? – Alexandre Santos – 2014-05-11T07:34:04.160

@AlexandreSantos "either" means at least one condition. The easiest way to understand what a positive integer is is to see the sample python code. – None – 2014-05-11T08:00:50.330

Have you got any tool for checking the correctness of the output? – James_pic – 2014-05-11T08:50:04.270

1What graphics card do you have and how much video memory does it have? – IchBinKeinBaum – 2014-05-11T16:57:19.887

@IchBinKeinBaum VGA compatible controller: Advanced Micro Devices, Inc. [AMD/ATI] RS880 [Radeon HD 4250] – None – 2014-05-12T18:43:35.873

@James_pic I am just comparing to my own slow code and the various submissions to each other. – None – 2014-05-12T18:43:58.827

I don't get deterministic output from the python script. Must solutions work for all possible outputs? Or should there be a constant seed value? – laindir – 2014-05-12T19:25:52.560

@laindir All possible outputs. – None – 2014-05-12T19:26:18.447

My script is IO bound, so what storage does your system have and will the file be cached in memory first or read directly from disk? – NPSF3000 – 2014-05-12T23:19:47.617

1@NPSF3000 I run sync && sudo bash -c 'echo 3 > /proc/sys/vm/drop_caches' before each test. I have a standard hard disk drive. – None – 2014-05-13T08:18:15.093

Thanks, my version should now be accpeting of cmd line arguments (inputFileName outPutFileName) – NPSF3000 – 2014-05-13T12:52:26.733

Answers

10

C, ~ 7 4.1 seconds

Radix sort on T, then walk through the array looking for matches.

It is fast because it is cache friendly. The radix sort reasonably so, and the final walk very. I have to check each row against about 100 others, but they are all adjacent in the cache.

Added: I no longer have to check each row against a scan of 100 other rows. A small table of counts of low order bits of b's in the window suffices to eliminate most instances of this scan.

Now about 1/2 time parsing, 1/3 time sorting, 1/6 time doing the actual matching.

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>

// B = # of bits per radix pass
// R = # of radix passes
#define B 9
#define R 3
#define M ((1<<B)-1)
#define MAXN 50000000

int count[R][1<<B];

typedef struct {
  int a,b,t,print;
} entry;

entry A[MAXN];
entry C[MAXN];

// Sized to fit well in L1 cache
unsigned char bcount[16384];

int main(int argc, char *argv[]) {
  FILE *f = fopen(argv[1], "r");
  fseek(f, 0, SEEK_END);
  int size = ftell(f);
  fclose(f);

  int fd = open(argv[1], O_RDONLY);
  const char *p = (const char*)mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
  const char *endp = p + size;

  // parse, insert into array
  int n = 0;
  while(p < endp) {

    // parse line
    int a = 0;
    while(*p != ' ') {
      a *= 10;
      a += *p - '0';
      p++;
    }
    p++;
    int b = 0;
    while(*p != ' ') {
      b *= 10;
      b += *p - '0';
      p++;
    }
    p++;
    int t = 0;
    while(*p != '\n') {
      t *= 10;
      t += *p - '0';
      p++;
    }
    p++;

    // insert it
    if(n == MAXN) {
      printf("too many elements\n");
      exit(1);
    }
    A[n].a = a;
    A[n].b = b;
    A[n].t = t;
    n++;

    // compute counts for radix sort
    count[0][t&M]++;
    count[1][(t>>B)&M]++;
    count[2][t>>2*B]++;
  }

  // accumulate count entries
  for(int r = 0; r < R; r++) {
    for(int i = 0; i < M; i++) {
      count[r][i+1]+=count[r][i];
    }
  }

  // radix sort, 3 rounds
  for(int i = n-1; i >= 0; i--) {
    C[--count[0][A[i].t&M]] = A[i];
  }
  for(int i = n-1; i >= 0; i--) {
    A[--count[1][(C[i].t>>B)&M]] = C[i];
  }
  for(int i = n-1; i >= 0; i--) {
    C[--count[2][A[i].t>>2*B]] = A[i];
  }

  // Walk through array (now sorted by T) and find matches.
  // We maintain a window of T values that might match.
  // To facilitate finding matches within that window, bcount
  // keeps track of a count of how many b's in that window
  // have the given low 14 bits.
  int j = 0;
  for(int i = 0; i < n; i++) {
    int a = C[i].a;
    int t = C[i].t;
    while(C[j].t <= t - 100) {
      int x = C[j].b & 16383;
      if(bcount[x] != 255) bcount[x]--;
      j++;
    }
    if(bcount[a & 16383] > 0) {
      // somewhere in the window is a b that matches the
      // low 14 bits of a.  Find out if there is a full match.
      for(int k = j; k < i; k++) {
        if(a == C[k].b)
          C[k].print = C[i].print = 1;
      }
    }
    int x = C[i].b & 16383;
    if(bcount[x] != 255) bcount[x]++;
  }
  for(int i = 0; i < n; i++) {
    if(C[i].print)
      printf("%d %d %d\n", C[i].a, C[i].b, C[i].t);
  }
}

Keith Randall

Posted 2014-05-05T20:12:10.653

Reputation: 19 865

This is equal first. I am surprised radix sort is so fast as normally you think of it as having terrible cache performance. I think I will need to test in single user mode to tell them apart as the timings are not exactly the same on each run even with the same test file. – None – 2014-05-13T08:19:54.910

YES! I love it. I had a feeling that cache locality would make joining on T faster, but I always figured the sort stage would offset any gains. Using Radix sort pretty much eliminates that. – James_pic – 2014-05-13T09:42:20.287

Radix sort works well in a cache because there is one stream of reads and N streams of writes (in my code, N=512). As long as your cache has N+1 cache lines, everything can remain in cache. – Keith Randall – 2014-05-13T17:18:54.967

Well damn. I literally just created filter.c to do the same thing, came to the question and found this. +1 – Geobits – 2014-05-13T21:51:32.990

@Geobits The top two submissions basically take the same amount of time. You could submit yours and we would have a three way race :) (Also, I plan to post a follow up challenge soon on the same topic.) – None – 2014-05-14T09:53:26.857

Your entry was just as fast as James_pic's but I couldn't award the bounty to both so I gave it to the first one. I hope that's OK. I am about to make a follow up question which I hope you will find fun too. – None – 2014-05-14T20:19:40.377

I noticed faster parsing times when doing the integer calculation in one line. Try:10 * temp + (*(p++) - '0'); – wolfhammer – 2014-05-14T21:02:10.300

For some reason a for loop was even faster. for(temp=0;*p != ' ';temp = 10 * temp + (*(p++) - '0')) {} lines[i].a = temp; p++; – wolfhammer – 2014-05-14T21:14:21.970

Absurdly fast. I couldn't even get parse time under 30s! – primo – 2014-05-15T04:34:33.037

I was just going back to this code and changed the data creation to be nolines = 5000000 and then print random.randint(0,100*nolines-1), random.randint(0,100*nolines-1), random.randint(0,100*nolines-1) . That is I multiplied the range by 100. Your code now runs very slowly. Do you know why by any chance? – None – 2014-09-05T12:03:19.367

1@Lembik: the code as is only sorts B*R=27 bit numbers. You now have 29 bit numbers - you need one more pass (R++) or one more bit per pass (B++). B++ is probably easier, R is hardcoded in a few unrolled loops. – Keith Randall – 2014-09-05T16:25:56.513

7

Scala 2.10 - 0:41

The problem is basically:

select * from data x, data x where x.a = y.b and 0 <= x.t - y.t and x.t - y.t < 100

Most RDBMSs would notice that the join from x.a to y.b has the highest specificity, and plan this as a hash join.

So that's what we'll do. We create a hashtable of the data on a, hash join it with the same table on b, and filter on the difference in t.

import scala.io.Source
import scala.reflect.ClassTag
import java.io._

object Filterer {
  def roundUpToNextPowerOfTwo(x: Int) = {
    // blatantly stolen from http://bits.stephan-brumme.com/roundUpToNextPowerOfTwo.html
    var y = x - 1
    y |= y >> 1
    y |= y >> 2
    y |= y >> 4
    y |= y >> 8
    y |= y >> 16
    y + 1
  }

  // We hash join the array with itself, a to b, and emit both rows if t is within 100. 50m records should fit into 8GB OK.
  def main(args: Array[String]): Unit = {
    val input = Source.fromFile(args(0), "ASCII").getLines()
    val output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args(1)), "US-ASCII"))
    try {
      val data1: Array[Row] = input.map{line =>
        Row(line)
      }.toArray

      /*
       * In theory, data1 and data2 could be created in parallel, but OpenHashMultiMap needs
       * to know its size at creation time, to sidestep the need for rehashing. I could just
       * hard-code 50 million (the size of the data in the challenge), but that seems dishonest.
       */
      val data2 = new OpenHashMultiMap[Int, Row](roundUpToNextPowerOfTwo(data1.size) * 2, -1)
      for (r <- data1) data2.insert(r.a, r) // data2 is hashed by a

      for (row1 <- data1.par) {
        val Row(a, b, t) = row1
        for (Row(c, d, u) <- data2.get(b) if (0 <= u - t) && (u - t < 100)) {
          // The conditions are symmetric, so if row1 matches, so does row2
          output.write(s"$a $b $t\n$c $d $u\n")
        }
      }
    } finally {
      output.close()
    }
  }
}

object Row {
  def apply(data: String): Row = {
    val l = data.length
    var i = 0
    var a = 0
    var b = 0
    var c = 0
    while (data.charAt(i) != ' ') {
      a = a * 10 + (data.charAt(i) - '0')
      i += 1
    }
    i += 1
    while (data.charAt(i) != ' ') {
      b = b * 10 + (data.charAt(i) - '0')
      i += 1
    }
    i += 1
    while (i < l) {
      c = c * 10 + (data.charAt(i) - '0')
      i += 1
    }
    Row(a, b, c)
  }
}

final case class Row(a: Int, b: Int, t: Int)

/*
 * None of the standard Java or Scala collections are particularly efficient as large MultiMaps,
 * so we write our own. We use open hashing with quadratic probing.
 */
class OpenHashMultiMap[@specialized(Int) K: ClassTag, V: ClassTag](capacity: Int, default: K) {
  require((capacity & (capacity - 1)) == 0) // Power of 2 capacity
  private val keys = Array.fill(capacity)(default)
  private val values = new Array[V](capacity)
  private val mask = capacity - 1

  private def hash(k: K) = {
    // Hash mingling - Int has a particularly poor hash
    k.hashCode * 428916315
  }

  def insert(k: K, v: V) = {
    var found = false
    var loc = hash(k) & mask
    var inc = 0
    while (inc <= capacity && !found) {
      loc = (loc + inc) & mask
      inc += 1
      found = keys(loc) == default
    }
    keys(loc) = k
    values(loc) = v
  }

  def get(key: K) = new Traversable[V] {
    override def foreach[U](f: V => U) = {
      var break = false
      var loc = hash(key) & mask
      var inc = 0
      while (inc <= capacity && !break) {
        loc = (loc + inc) & mask
        inc += 1
        val k = keys(loc)
        if (key == k) f(values(loc))
        else if (k == default) break = true
      }
    }
  }
}

Compile with:

scalac Filterer.scala

And run with:

scala -J-server -J-XX:+AggressiveOpts -J-Xms6g -J-Xmx6g Filterer input_file.dat output_file.txt

On my machine, this runs in 2 minutes 27.

Still, it might be interesting to try the approach from @Lembik's answer, but in a faster language. It corresponds to something like a merge join on t. On paper, it should be slower, but it has better cache locality, which may push it ahead.

Update

I've managed to shave a large chunk of time off with a surprisingly small change - a better hash mingler. The hash map is very sensitive to hash clumping, so this change brings it down to 1:45 on my machine.

Most of that time is spent reading the data into an array.

I'm curious as to why my data reading code is so much slower than @Geobits. It takes my code 70 seconds to read the data in - longer than @Geobits entire program, once the Thread.start bug is fixed. I'm tempted to steal @Geobits approach to reading data, but I'm not sure how the Stack Exchange gods would feel about that.

Update 2

I've made further improvements, this time to the data reader. Using pattern matching and monad operations inside the loop was hurting performance, so I've simplified it. I think scala.io.Source is the next bottleneck to tackle.

It's down to 1:26 on my machine now.

Update 3

Got rid of probe from OpenHashMultiMap. The code is now more java-ish, and runs in 1:15.

Update 4

I'm now using an FSM to parse input. Run time is down to 0:41

James_pic

Posted 2014-05-05T20:12:10.653

Reputation: 3 988

I get James_pic.scala:42: error: ')' expected but string literal found. output.write(s"$a $b $t\n$c $d $u\n") ^ one error found. This is on Scala compiler version 2.9.2 – None – 2014-05-08T16:39:02.977

1I got it to work with 2.10.3. It's a very nice solution although my poor computer is more or less unusable for a minute or so afterwards as it tries to deallocate 6GB of RAM. – None – 2014-05-08T18:36:20.017

Yes, sorry. I figured you might have that issue. Ubuntu still ships with Scala 2.9, and string interpolation needs 2.10 or higher. I suspect it'll be faster still under Java 8, but Ubuntu only ships with 7, and that's a world of pain that you don't need! – James_pic – 2014-05-09T08:03:01.770

Re input: I don't always use StringTokenizer, but when I do, I parse millions of strings.

– Geobits – 2014-05-09T14:22:47.457

@Geobits Yeah, String.split is currently a bottleneck, but StringTokenizer isn't much better right now - allocating in a tight inner loop is hurting my already strained GC. I'm working on an FSM which seems to have promise (whilst being complete overkill) – James_pic – 2014-05-09T14:50:13.610

I managed to speed it up by just using time scala -J-Xmx6g Filterer largefile.file output.txt – None – 2014-05-10T18:50:21.293

@Lembik, was that using the latest version of the code? It's had a few upgrades since my original submission. – James_pic – 2014-05-11T08:48:37.917

I am using your latest code. It is much slower using -J-server -J-XX:+AggressiveOpts -J-Xms6g for me. – None – 2014-05-12T18:45:43.250

I think neither this code nor your C code allow T - U = 0. – None – 2014-05-12T20:15:51.567

You're right. That'll teach me for not reading the instructions properly. It's a one-letter change in both cases, so I'll go make it. – James_pic – 2014-05-13T09:34:59.117

6

Java : 1m54s

(On my i7)

Since every match will be within 100 t of its mate, I decided to bucket the inputs by t. There is a bucket for each 100, so to check a number, it only has to check against +/-1 buckets.

On average, each bucket only contains 100 entries, so it doesn't take long to scan a few buckets for each. Well over half the time is spent reading and bucketing, matching takes only 40 seconds or so.

Note: Depending on your JVM setup, you may need to increase the heap size. This also assumes file name of test.file. Simply change it on line 24 if that's not the case.

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Filter_26643 {

    final static int numThreads = 8; 
    final static int numInputs = 50000000;
    final static int bucketSize = 100;
    final static int numBuckets = numInputs/bucketSize;
    ArrayList<ArrayList<int[]>> buckets;

    public static void main(String[] args) {
        new Filter_26643().run();
    }

    void run(){
        try{
            buckets = new ArrayList<ArrayList<int[]>>(numBuckets);
            for(int i=0;i<numBuckets;i++)
                buckets.add(new ArrayList<int[]>(bucketSize*2));

            BufferedReader reader = new BufferedReader(new FileReader("test.file"));
            int c=0,e[];
            while(c++<numInputs){
                StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
                e = new int[] {
                                Integer.parseInt(tokenizer.nextToken()),
                                Integer.parseInt(tokenizer.nextToken()),
                                Integer.parseInt(tokenizer.nextToken())
                                }; 
                buckets.get(e[2]/100).add(e);
            }
            reader.close();

            MatchThread[] threads = new MatchThread[numThreads];
            for(int i=0;i<numThreads;i++){
                threads[i] = new MatchThread(i);
                threads[i].start();
            }
            for(int i=0;i<numThreads;i++)
                threads[i].join();

        } catch(Exception e){
            e.printStackTrace();
        }
    }

    class MatchThread extends Thread{
        int index;

        public MatchThread(int index){
            this.index = index;
        }

        @Override
        public void run() {
            for(int i=index;i<numBuckets;i+=numThreads){
                int max = i+2 >= numBuckets ? numBuckets : i+2;
                int min = i-1 < 0 ? i : i-1;
                for(int[] entry : buckets.get(i)){
                    outer:
                    for(int j=min;j<max;j++){
                        ArrayList<int[]> bucket = buckets.get(j);
                        for(int[] other : bucket){
                            if(((entry[0]==other[1] && entry[2]-other[2]<100 && entry[2]>=other[2]) || 
                                (entry[1]==other[0] && other[2]-entry[2]<100 && other[2]>=entry[2]))
                                && entry != other){
                                 System.out.println(entry[0] + " " + entry[1] + " " + entry[2]);
                                 break outer;
                            }
                        }                           

                    }   
                }
            }
        }
    }
}

Geobits

Posted 2014-05-05T20:12:10.653

Reputation: 19 061

After 5 and a half minutes I get Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded as you suggested. How much do I have to increase the heap size to ? – None – 2014-05-09T06:26:13.890

You've made the canonical threading mistake! On line 40, you've used Thread::run, not Thread.start, so it's all running on the main thread. With Thread::start, run time drops from 1:38 to 0:46 on my machine. – James_pic – 2014-05-09T08:27:38.877

@James_pic Did you increase the heap size? Also, how does 0:46 compare to the time for sort -n test.file on your computer (if you can arrange for it not to be already in RAM)? – None – 2014-05-09T08:36:59.523

The machine I'm currently at is a Windows box, so I can't measure sort time. I bumped the heap up to 6G, same as mine (you said you had 8G, so it seemed a sensible guess). – James_pic – 2014-05-09T08:50:11.353

@James_pic So I did... I thought it was going a bit slower than it should, lol. Will post an updated time for my machine when I can run it again >< – Geobits – 2014-05-09T13:13:32.230

@Lembik Using Runtime methods I'm showing a bit under 3GB used, but bumping it higher than that certainly won't hurt. I set mine to 6GB as well. – Geobits – 2014-05-09T13:15:10.723

What's very impressive about this is that it is as fast as sorting. – None – 2014-05-10T18:50:51.127

@Lembik If this were done in a native executable I'm sure it would be a bit faster still. The built-in sort most likely uses a general-purpose sort like quick/merge, since it can't know the inputs are capped at 50M. If you ran a straight bucket/radix sort, I'm sure it would blow it out of the water. Still, most of the time spent is on input in both cases. I/O only gets so fast. – Geobits – 2014-05-10T20:19:14.433

@Geobits True although wc is much quicker so just input can be done fast. – None – 2014-05-10T20:28:41.053

@Lembik In principle, the best algorithms should be faster than sort. The fastest sort algorithms are O(n*log(n)), whereas mine and Geobits algorithms are O(n). As you say, wc is a better comparison, as word counting can be done in O(n) – James_pic – 2014-05-11T08:46:27.613

1Incidentally @Geobits, I like this algorithm. You get most of the benefits of a merge join, without the overhead of sorting - it's sort of like a pigeonhole-sorted merge join. – James_pic – 2014-05-11T08:54:43.313

6

C - 12 seconds

I decided to port my Scala answer to C, to see how much more performance I could get.

It's more or less the same approach (build an open hash table on a), except that I skip the step where I build the initial array, and iterate from the hash table directly (for some reason I could never get this approach to perform in Scala - I suspect JVM inlining was to blame).

I didn't bother with threads, as it's a pain to do portably.

The code is:

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

// Should be 37% occupied with 50m entries
#define TABLE_SIZE 0x8000000
#define MASK (TABLE_SIZE - 1)
#define BUFFER_SIZE 16384
#define END_OF_FILE (-1)
#define DEFAULT_VALUE (-1)

typedef struct Row {
  int32_t a;
  int32_t b;
  int32_t t;
} Row;

int32_t hash(int32_t a) {
  return a * 428916315;
}

void insert(Row * table, Row row) {
  long loc = hash(row.a) & MASK; // Entries are hashed on a
  long inc = 0;
  while (inc <= TABLE_SIZE) {
    loc = (loc + inc) & MASK;
    inc++;
    if (table[loc].a == DEFAULT_VALUE) {
      table[loc] = row;
      break;
    }
  }
}

int readChar(FILE * input, char * buffer, int * pos, int * limit) {
  if (*limit < *pos) {
    return buffer[(*limit)++];
  } else {
    *limit = 0;
    *pos = fread(buffer, sizeof(char), BUFFER_SIZE, input);
    if (*limit < *pos) {
      return buffer[(*limit)++];
    } else return END_OF_FILE;
  }
}

void readAll(char * fileName, Row * table) {
  char* buffer = (char*) malloc(sizeof(char) * BUFFER_SIZE);
  int limit = 0;
  int pos = 0;

  FILE * input = fopen(fileName, "rb");

  int lastRead;
  Row currentRow;
  uint32_t * currentElement = &(currentRow.a);

  // As with the Scala version, we read rows with an FSM. We can
  // roll up some of the code using the `currentElement` pointer
  while (1) {
    switch(lastRead = readChar(input, buffer, &pos, &limit)) {
      case END_OF_FILE:
        fclose(input);
        return;
      case ' ':
        if (currentElement == &(currentRow.a)) currentElement = &(currentRow.b);
        else currentElement = &(currentRow.t);
        break;
      case '\n':
        insert(table, currentRow);
        currentRow.a = 0;
        currentRow.b = 0;
        currentRow.t = 0;
        currentElement = &(currentRow.a);
        break;
      default:
        *currentElement = *currentElement * 10 + (lastRead - '0');
        break;
    }
  }
  //printf("Read %d", lastRead);
}

int main() {
  Row* table = (Row*) malloc(sizeof(Row) * TABLE_SIZE);
  memset(table, 255, sizeof(Row) * TABLE_SIZE);

  readAll("test.file", table);

  // We'll iterate through our hash table inline - passing a callback
  // is trickier in C than in Scala, so we just don't bother
  for (size_t i = 0; i < TABLE_SIZE; i++) {
    Row * this = table + i;
    if (this->a != DEFAULT_VALUE) {
      // Lookup entries `that`, where `that.a == this.b`
      long loc = hash(this->b) & MASK;
      long inc = 0;
      while (inc <= TABLE_SIZE) {
        loc = (loc + inc) & MASK;
        inc++;
        Row * that = table + loc;
        if ((this->b == that->a) && (0 <= that->t - this->t) && (that->t - this->t < 100)) {
          // Conditions are symmetric, so we output both rows
          printf("%d %d %d\n", this->a, this->b, this->t);
          printf("%d %d %d\n", that->a, that->b, that->t);
        }
        else if (that->b == DEFAULT_VALUE) break;
      }
    }
  }

  free(table);
  return 0;
}

Compile with:

gcc -std=c99 -O3 -m64 filter.c

And run with:

./a.out

The location of the test file is hard-coded as "test.file".

Once again, reading the data takes up most of the time (just under 9 seconds). Matching takes the rest of the time.

Again, it'll be interesting to see how this fares against Scott Leadley's answer, which uses the same language but a different strategy. Scott is joining on T, which in principle means he'll have more to join against, but again, joining on T gives better cache locality.

James_pic

Posted 2014-05-05T20:12:10.653

Reputation: 3 988

I get James_pic.c: In function ‘readAll’: James_pic.c:67:28: warning: comparison of distinct pointer types lacks a cast [enabled by default] if (currentElement == &(currentRow.a)) currentElement = &(currentRow.b); – None – 2014-05-12T18:35:04.880

I get very slightly different outputs from your scala and C code. In fact only one line is different. I just did diff <(sort -n James_pic-c.out) <(sort -n James_pic-scala.out) – None – 2014-05-12T18:49:03.280

This will fail for inputs in which a given a value occurs n times where n >= BUFFER_SIZE + 2 – laindir – 2014-05-12T19:28:41.880

I think it's just that you have <=100 in this code and < 100 in the scale code. – None – 2014-05-12T19:54:13.887

@Lembik I think you're right. Those darned off-by-one errors! – James_pic – 2014-05-13T09:37:15.650

@Iaindir - You're right - I was comparing to BUFFER_SIZE when I should have been comparing to TABLE_SIZE. It's fixed now. – James_pic – 2014-05-13T09:57:27.453

2

perl, 17m46s on a core i7 w/ 8GB mem

First, we use sort -n -k3 to get the most important field in order, taking advantage of the built-in parallelism on modern versions of sort(1). Then, since perl is greatly hampered by the fact that a simple scalar takes on the order of 80 bytes each (50 million * 3 * 80 is too much - at least 12GB), we slurp the output in to a 50 million * 12 byte array (12 bytes per line, each line contains 3 integers that can be represented as a 32 bit integer). Then we fire off 8 threads covering (roughly) 1/8th of the data each (+ some overlap).

#!perl

use strict;
use warnings;

# find lines s.t. $lines[$M]->{a} == $lines[$N]->{b} and
#                 0 <= $lines[$M]->{t} - $lines[$N]->{t} < 100
# OR              $lines[$M]->{b} == $lines[$N]->{a} and
#                 0 <= $lines[$N]->{t} - $lines[$M]->{t} < 100

my $infile = shift;
open(my $fh, "sort -n -k3 $infile |") || die "open sort pipe: $@";

my @lines;
my $bytes_per_int = 4;
my $bytes_per_line = $bytes_per_int * 3;
my $nlines = 50_000_000;
my $buf = "\0" x ($nlines * $bytes_per_line);
my $ln = 0;
my $nprocs = 8;
my $last_group_start = 0;
my $this_group_start;
my $group = $nlines / $nprocs;
my @pids;
while(<$fh>) {
  my ($A, $B, $T) = split/\s+/;
  substr($buf, $ln * $bytes_per_line, $bytes_per_line, pack "L3", ($A, $B, $T));
  if( defined $this_group_start ) {
    if( $T - $last_group_start >= $group + 100 ) {
      if(my $pid = fork()) {
        push @pids, $pid;
        $last_group_start = $this_group_start;
        undef $this_group_start;
      } else {
#warn "checking $last_group_start - $ln...\n";
        for(my $l=$last_group_start; $l<=$ln; ++$l) {
          my $lpos = $l * $bytes_per_line;
          my ($A, $B, $T) = unpack "L3", substr($buf, $lpos, $bytes_per_line);
          my ($lA, $lB);
          my $lT = $T;
          for(my $lb=$l; $lb>=$last_group_start && $T - $lT <= 100; $lb--, $lpos -= $bytes_per_line) {
            ($lA, $lB, $lT) = unpack "L3", substr($buf, $lpos, $bytes_per_line);
            if($A == $lB || $B == $lA) {
              #print "($last_group_start) $A $B $T matches $lA $lB $lT\n";
              print "$lA $lB $lT\n$A $B $T\n";
            }
          }
        }
        exit;
      }
    }
  } elsif( !defined $this_group_start && $T - $last_group_start >= $group ) {
    $this_group_start = $ln;
  }
  $ln++;
}

waitpid $_, 0 for @pids;

Unsorted output:

8455767 30937130 50130
20468509 8455767 50175
47249523 17051933 111141
17051933 34508661 111215
39504040 36752393 196668
42758015 39504040 196685
25072294 28422439 329284
35458609 25072294 329375
45340163 42711710 6480186
39315845 45340163 6480248
1435779 49643646 12704996
38229692 1435779 12705039
18487099 24556657 6665821
24556657 28498505 6665884
6330540 35363455 18877328
22500774 6330540 18877347
10236123 22026399 598647
39941282 10236123 598717
45756517 24831687 6726642
34578158 45756517 6726670
29385533 7181838 621179
7181838 29036551 621189
40647929 11895227 25075557
11895227 1900895 25075652
17921258 42642822 18935923
40140275 17921258 18935949
44573044 38139831 12899467
38139831 1321655 12899468
11223983 1788656 12920946
1788656 21905607 12921040
1357565 8148234 801402
8148234 46556089 801498
30929735 303373 19105532
31258424 30929735 19105543
34899776 9929507 6990057
9929507 49221343 6990078
49779853 43951357 25306335
41120244 49779853 25306424
6177313 41551055 25343755
24462722 6177313 25343804
16392217 32915797 31472388
32915797 19696674 31472479
6834305 36264354 25440771
44983650 6834305 25440800
26559923 47360227 19356637
47360227 49749757 19356700
33018256 36233269 37654651
36233269 5459333 37654671
6932997 23123567 25502355
23123567 7882426 25502356
5878434 43421728 25510707
43421728 40827189 25510765
38695636 33504665 1099515
13504170 38695636 1099605
32832720 40188845 37689854
8335398 32832720 37689927
35858995 41917651 1130028
41917651 28797444 1130096
47102665 6796460 43806189
6796460 6113288 43806229
21248273 5422675 43819677
48011830 21248273 43819728
32187324 39177373 25624030
39177373 42539402 25624102
41722647 14351373 25626925
14351373 45070518 25627013
22298566 25860163 37862683
2273777 22298566 37862692
10617763 32776583 7561272
35581425 10617763 7561279
18526541 18709244 31960780
18709244 32777622 31960867
36976439 24222624 31973215
24222624 9534777 31973262
25751007 11612449 38066826
43652333 25751007 38066923
8303520 2615666 7633297
2615666 29961938 7633357
22317573 31811902 31982722
14298221 22317573 31982819
43089781 7653813 44154683
8732322 43089781 44154769
24227311 43800700 13711475
40906680 24227311 13711539
48061947 30109196 7660402
43993467 48061947 7660488
29580639 5292950 38140285
5292950 21293538 38140356
17646232 47737600 32058831
47737600 42934248 32058836
13262640 23462343 1617194
23462343 1901587 1617259
5150775 7046596 44270140
7046596 22819218 44270181
17749796 34924638 32171251
8386063 17749796 32171346
30095973 12202864 38257881
12202864 42679593 38257912
10353022 40646034 26158412
40646034 36237182 26158412
8416485 16245525 32223010
16245525 32420032 32223081
20420340 1371966 7893319
1371966 2031617 7893335
2864137 20279212 26199008
29145409 2864137 26199080
29141766 19729396 44433106
44115780 29141766 44433141
6513924 34515379 32283579
12686666 6513924 32283636
20116056 49736865 44464394
49736865 47918939 44464416
38212450 3465543 32302772
3465543 39217131 32302873
12019664 37367876 44485630
3639658 12019664 44485639
18053021 1279896 7973955
2220749 18053021 7974031
19701732 12984505 1857435
24625926 19701732 1857528
9876789 34881917 26285125
27687743 9876789 26285134
5696632 6064263 44534580
34888313 5696632 44534629
14865531 46418593 38457138
5929897 14865531 38457191
44378135 4051962 38485208
4051962 10804515 38485308
11865822 21793388 14142622
7760360 11865822 14142649
32333570 24478420 44702533
24478420 23749609 44702588
29098286 25015092 44723985
32171647 29098286 44723985
20522503 20522503 2127735
20522503 20522503 2127735
22597975 20938239 8260902
20938239 48618802 8260905
8310032 34659671 2153994
34659671 25406149 2154075
49085033 5708432 26644257
5708432 32265692 26644305
18751513 18226037 32726402
18226037 33885794 32726424
45877488 23211339 20566948
23211339 26209405 20567002
48554034 25770643 38853402
9683274 48554034 38853467
9770420 14556349 2309265
27255587 9770420 2309324
32926392 16744099 44954824
24840989 32926392 44954840
29066838 49434549 26755357
49434549 12635292 26755407
21927714 32352409 20626921
32352409 15895076 20626932
7422009 23559357 14550898
32743911 7422009 14550982
38816601 5850890 26851026
5850890 32996623 26851107
42148171 47021378 26872907
47021378 32628418 26872908
9850929 10501741 32998960
10501741 24899993 32999043
27491904 4393602 33033499
4393602 17712085 33033570
37978226 42226216 39114479
42226216 2511412 39114525
42859989 49908919 45241083
48131208 42859989 45241088
39753103 30674979 14807321
30674979 45637890 14807371
30154199 11988643 2641926
11988643 11241926 2641976
7191871 13518594 45370275
13518594 45354921 45370344
54745 19711137 8871851
24814115 54745 8871937
38770495 34574748 2756244
41962321 38770495 2756337
26229406 39306415 21057327
10735951 26229406 21057347
46704290 11506122 39359422
18181795 46704290 39359481
38796645 28410469 45452212
28410469 13478996 45452222
412456 27727741 39466147
27727741 19639136 39466226
24470627 13030982 21266756
13030982 21713410 21266825
6058593 23139172 27435254
19236012 6058593 27435303
14457750 39190113 39701131
30253141 14457750 39701227
26898421 39016446 45812750
40952330 26898421 45812829
18647206 27663400 45817956
27663400 21728474 45817989
5559358 41319001 33664547
41319001 37210583 33664636
29066692 30653068 39759813
30653068 38963132 39759856
12086617 49971187 3232640
49971187 32302154 3232649
12008399 13656671 3239395
43088998 12008399 3239439
10061612 38594475 39804389
38594475 6327106 39804405
16703379 21150436 39851057
21150436 34093320 39851136
1035486 4199407 3314170
26974438 1035486 3314196
21869320 14532221 33851404
15208937 21869320 33851473
38840190 4742355 3402401
4742355 46055869 3402462
34432016 8734566 39966972
27614117 34432016 39967002
9988172 49209666 46063078
49209666 29374155 46063087
3208946 47030309 21722002
47030309 39809983 21722030
10928661 46423741 3496854
46423741 29486710 3496862
42464855 22978174 46154827
22978174 3814497 46154901
47090840 16768393 46169667
39523858 47090840 46169714
28186104 11618234 34024001
11618234 33711158 34024019
45471813 37332848 3585557
37332848 4607526 3585600
14885742 38990612 15863749
38990612 3710491 15863779
42391514 33643913 22005928
33643913 32254640 22006022
4299590 19482026 34202327
19482026 35838894 34202406
24298776 16276160 3858885
16276160 3198758 3858958
29322567 12536696 40433239
12536696 26083938 40433317
16080151 9648322 22221443
9648322 43846385 22221458
999302 19218350 10078183
10296062 999302 10078189
40544377 34492433 34463953
19908418 40544377 34463993
10765321 45143043 34542584
39154522 10765321 34542646
48642526 31097951 4104790
2940654 48642526 4104887
26972730 47422139 46846889
39228577 26972730 46846901
13788696 11503551 34728076
11503551 9151627 34728130
8676030 30463644 10406398
15204754 8676030 10406405
42984277 41087708 34805119
48741576 42984277 34805143
29634598 2151247 22699609
12264074 29634598 22699614
47525963 48470003 16667878
48470003 4566846 16667953
9725907 43325112 4498307
26465445 9725907 4498368
306967 11708860 10633595
11708860 31017081 10633669
39420965 46595640 41089015
46595640 41260374 41089048
29232745 39705052 16754836
4739295 29232745 16754840
35246405 42811088 41273637
48986699 35246405 41273719
2398239 36985098 35181790
36985098 7460784 35181841
18955749 23678549 35221035
47264406 18955749 35221129
18105816 26003002 17044057
26003002 17467477 17044087
14430126 46039962 47492180
46039962 29118827 47492275
30329324 40926812 41425850
43304610 30329324 41425912
34966996 36567528 17095113
3967517 34966996 17095144
42829171 42530474 23209891
25923738 42829171 23209967
28187681 26297990 35474412
48986691 28187681 35474475
5707126 41598794 17298139
40466899 5707126 17298188
28838696 30725820 5142797
30725820 35360418 5142798
44642019 42570370 17339657
42570370 19022469 17339727
42193681 8389736 17386517
48906013 42193681 17386586
42303185 30337820 41795129
30337820 42473956 41795170
30935782 8441903 17515229
41549758 30935782 17515275
41239019 10011768 23619001
10011768 25386353 23619062
494288 13341166 29815779
49113152 494288 29815876
7106674 26227442 29833029
47459682 7106674 29833047
17246497 35389391 17628365
35389391 34005133 17628371
23347674 48243185 17792799
48243185 22907892 17792836
21852744 1662414 36088704
8040124 21852744 36088775
32384657 27122374 36100767
24980361 32384657 36100782
31016207 26300043 42222489
26300043 36869529 42222544
17178756 44315094 42223989
44315094 11222466 42224042
34139317 39164101 36197907
39164101 27563542 36197947
31638631 22215137 17999735
22215137 10771707 17999769
30257199 32883043 24127009
32883043 179099 24127047
47774058 17451960 30283073
44583527 47774058 30283162
13816647 12695130 24145102
12695130 42284941 24145188
42749234 20004242 5893793
20004242 38129713 5893819
22210359 22178109 18109989
22178109 112961 18110049
42509645 28599506 42508465
28599506 3722411 42508513
34412629 22547405 48610262
22547405 16664124 48610296
2330283 32267749 24256113
35915758 2330283 24256157
44560231 49353986 12101694
6471293 44560231 12101780
23289721 8186827 30407293
10624448 23289721 30407389
12329357 35765163 30560085
4511908 12329357 30560158
31332240 39704929 12269193
39704929 47770487 12269249
22286152 22082044 36734758
22082044 25076919 36734833
47381309 9459604 36735886
9459604 31071680 36735890
43832763 45342283 30707519
45342283 26992816 30707602
2883029 18642608 42989696
14697025 2883029 42989793
15149987 40746227 24700535
40746227 34776566 24700549
2387554 49015265 43057085
49015265 21103141 43057139
23057202 13308993 30982514
34596334 23057202 30982553
44598498 31714790 43285828
18170064 44598498 43285841
38273701 11976319 31179763
15344094 38273701 31179764
3651338 27427037 37188945
12876654 3651338 37189007
10081580 3418061 37221143
3418061 38353019 37221143
172544 18699860 37295343
824744 172544 37295372
13914 8890169 37303853
8890169 14008003 37303898
18716557 29456130 49605004
29456130 16390535 49605083
15398102 22446674 43711290
22446674 38760679 43711383

I'm sure this would be an order of magnitude faster in C, but I probably won't take the time to do so.

skibrianski

Posted 2014-05-05T20:12:10.653

Reputation: 1 197

2I'm not sure your output is quite correct. Look at the first two rows: A = D = 8455767, but U = 50175, T = 50130, and so T - U = -45 – James_pic – 2014-05-08T14:25:09.607

2

C# - 30 Seconds

A different approach from most if I read right - I don't use any hash based structures.

I tend to get no results, not sure if this a statistical anomaly, or error in my reasoning. Fixed, comparison for binary sort was flawed.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace FilterFile
{
    class Program
    {
        const int COUNT = 50000000;

        static string inputFile = "data" + COUNT + ".txt";
        static string outputFile = "results.txt";

        static void Main(string[] args)
        {
            Console.WriteLine("Prepping Test");
            if (args.Length > 0) inputFile = args[0];
            if (args.Length > 1) outputFile = args[1];

            if (!File.Exists(inputFile))
            {
                Console.WriteLine(inputFile);

                File.WriteAllLines(inputFile,
                                     GenerateData(COUNT)
                                     .Select(r => string.Format("{0} {1} {2}", r.A, r.B, r.C)));
            }

            File.Delete("results.txt");

            Console.WriteLine("Starting Test \n\n");

            using (Timer.Create("Total Time"))
            {
                Row[] sortedA, sortedB;
                //http://codegolf.stackexchange.com/questions/26643/filter-a-large-file-quickly
                using (Timer.Create("Reading Data"))
                    FillData(out sortedA, out sortedB);

                using (Timer.Create("Parallel Sort A"))
                    ParallelSort.QuicksortParallel(sortedA);
                using (Timer.Create("Parallel Sort B"))
                    ParallelSort.QuicksortParallel(sortedB, (x, y) => x.B - y.B);

                object rLock = new object();
                List<Row> results = new List<Row>();

                var comparison = Comparer<Row>.Create((B, A) => B.B - A.A);
                using (Timer.Create("Compute Results"))
                    Parallel.ForEach(sortedA, row =>
                    //foreach (var row in sortedA)
                    {
                        var i = Array.BinarySearch(sortedB, row, comparison);
                        if (i < 0) return;

                        Row other;
                        bool solved = false;
                        for (var tempI = i; tempI < sortedB.Length && row.A == (other = sortedB[tempI]).B; tempI++)
                        {
                            var diff = row.C - other.C;
                            if (diff >= 0 && diff < 100)
                            {
                                lock (rLock) results.Add(row);
                                return;
                            }
                        }

                        for (var tempI = i - 1; tempI >= 0 && row.A == (other = sortedB[tempI]).B; tempI--)
                        {
                            var diff = row.C - other.C;
                            if (diff >= 0 && diff < 100)
                            {
                                lock (rLock) results.Add(row);
                                return;
                            }
                        }
                    });

                using (Timer.Create("Save Results"))
                {
                    File.WriteAllLines(outputFile, results.Select(r => r.ToString()));
                }
            }
        }

        private static void FillData(out Row[] sortedA, out Row[] sortedB)
        {
            var tempA = new Row[COUNT];
            var tempB = tempA;//new Row[COUNT];

            const int PARTITION_SIZE = 1 << 22;

            ReadAndSort(tempA, tempB, PARTITION_SIZE);

            sortedA = tempA;
            sortedB = new Row[COUNT];
            Array.Copy(sortedA, sortedB, COUNT);
            /*using (Timer.Create("MergeA"))
            {
                int destIndex = 0;
                int[][] partitions = Enumerable.Range(0, COUNT / PARTITION_SIZE + 1)
                    .Select(i => new[] { i * PARTITION_SIZE, Math.Min(i * PARTITION_SIZE + PARTITION_SIZE, COUNT) - 1 })
                    .ToArray();

                for (int i = 0; i < COUNT; i++)
                {
                    foreach (var partition in partitions)
                    {
                        while (partition[0] <= partition[1] && tempA[partition[0]].A == i)
                        {
                            sortedA[destIndex++] = tempA[partition[0]++];
                        }
                    }
                }
            }*/

            /*//Verify Paritioning Works
            var results = new List<Tuple<Row, int>> { Tuple.Create(tempA[0], 0) };
            for (int i = 1; i < tempA.Length; i++)
            {
                var r = tempA[i];
                if (r.A < tempA[i-1].A)
                    results.Add(Tuple.Create(r, i % PARTITION_SIZE));
            }
            results.ForEach(t => Console.WriteLine(t.Item1 + " " + t.Item2));*/
        }

        private static void ReadAndSort(Row[] tempA, Row[] tempB, int PARTITION_SIZE)
        {
            List<Task> tasks = new List<Task>();

            using (var stream = File.OpenRead(inputFile))
            {
                int b;
                int tempMember = 0;
                int memberIndex = 0;
                int elementIndex = 0;

                using (Timer.Create("Read From Disk"))
                    while ((b = stream.ReadByte()) >= 0)
                    {
                        switch (b)
                        {
                            case (byte)'\r':
                            case (byte)' ':
                                switch (memberIndex)
                                {
                                    case 0: tempA[elementIndex].A = tempMember; memberIndex = 1; break;
                                    case 1: tempA[elementIndex].B = tempMember; memberIndex = 2; break;
                                    case 2: tempA[elementIndex].C = tempMember; memberIndex = 0; break;
                                }
                                tempMember = 0;
                                break;
                            case (byte)'\n':
                                /*if (elementIndex % PARTITION_SIZE == 0 && elementIndex > 0)
                                {
                                    var copiedIndex = elementIndex;
                                    tasks.Add(Task.Run(() =>
                                    {
                                        var startIndex = copiedIndex - PARTITION_SIZE;
                                        Array.Copy(tempA, startIndex, tempB, startIndex, PARTITION_SIZE);
                                        ParallelSort.QuicksortSequentialInPlace(tempA, startIndex, copiedIndex - 1);
                                        ParallelSort.QuicksortSequentialInPlace(tempB, startIndex, copiedIndex - 1, (x, y) => x.B - y.B);
                                    }));
                                }*/
                                elementIndex++;
                                break;
                            default:
                                tempMember = tempMember * 10 + b - '0';
                                break;
                        }
                    }

                /* tasks.Add(Task.Run(() =>
                 {
                     elementIndex--;  //forget about the last \n
                     var startIndex = (elementIndex / PARTITION_SIZE) * PARTITION_SIZE;
                     Array.Copy(tempA, startIndex, tempB, startIndex, elementIndex - startIndex + 1);
                     ParallelSort.QuicksortParallelInPlace(tempA, startIndex, elementIndex);
                     ParallelSort.QuicksortSequentialInPlace(tempB, startIndex, elementIndex, (x, y) => x.B - y.B);
                 }));

                 using (Timer.Create("WaitForSortingToFinish"))
                     Task.WaitAll(tasks.ToArray());*/
            }
        }

        static Random rand = new Random();

        public struct Row : IComparable<Row>
        {
            public int A;
            public int B;
            public int C;
            public static Row RandomRow(int count)
            {
                return new Row { A = rand.Next(count), B = rand.Next(count), C = rand.Next(count) };
            }

            public int CompareTo(Row other)
            {
                return A - other.A;
            }

            public override string ToString()
            {
                return string.Format("{0} {1} {2}", A, B, C);
            }
        }

        public static Row[] GenerateData(int count)
        {
            var data = new Row[count];
            for (int i = 0; i < count; i++)
                data[i] = Row.RandomRow(count);
            return data;
        }

        public static Row[] GenerateSplitData(int count)
        {
            var data = new Row[count];
            for (int i = 0; i < count; i++)
                data[i] = Row.RandomRow(count);
            return data;
        }

        public class Timer : IDisposable
        {
            string message;
            Stopwatch sw;
            public static Timer Create(string message)
            {
                Console.WriteLine("Started: " + message);
                var t = new Timer();
                t.message = message;
                t.sw = Stopwatch.StartNew();
                return t;
            }
            public void Dispose()
            {
                Console.WriteLine("Finished: " + message + " in " + sw.ElapsedMilliseconds + "ms");
            }
        }

        // <summary> 
        /// Parallel quicksort algorithm. 
        /// </summary> 
        public class ParallelSort
        {
            const int SEQUENTIAL_THRESHOLD = 4096;
            #region Public Static Methods

            /// <summary> 
            /// Sequential quicksort. 
            /// </summary> 
            /// <typeparam name="T"></typeparam> 
            /// <param name="arr"></param> 
            public static void QuicksortSequential<T>(T[] arr) where T : IComparable<T>
            {
                QuicksortSequentialInPlace(arr, 0, arr.Length - 1);
            }

            /// <summary> 
            /// Parallel quicksort 
            /// </summary> 
            /// <typeparam name="T"></typeparam> 
            /// <param name="arr"></param> 
            public static void QuicksortParallel<T>(T[] arr) where T : IComparable<T>
            {
                QuicksortParallelInPlace(arr, 0, arr.Length - 1);
            }

            #endregion

            #region Private Static Methods

            public static void QuicksortSequentialInPlace<T>(T[] arr, int left, int right)
                where T : IComparable<T>
            {
                if (right > left)
                {
                    int pivot = Partition(arr, left, right);
                    QuicksortSequentialInPlace(arr, left, pivot - 1);
                    QuicksortSequentialInPlace(arr, pivot + 1, right);
                }
            }

            public static void QuicksortParallelInPlace<T>(T[] arr, int left, int right)
                where T : IComparable<T>
            {
                if (right > left)
                {
                    if (right - left < SEQUENTIAL_THRESHOLD)
                        QuicksortSequentialInPlace(arr, left, right);
                    else
                    {
                        int pivot = Partition(arr, left, right);
                        Parallel.Invoke(() => QuicksortParallelInPlace(arr, left, pivot - 1),
                                        () => QuicksortParallelInPlace(arr, pivot + 1, right));
                    }
                }
            }

            private static void Swap<T>(T[] arr, int i, int j)
            {
                T tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
            }

            private static int Partition<T>(T[] arr, int low, int high)
                where T : IComparable<T>
            {
                // Simple partitioning implementation 
                int pivotPos = (high + low) / 2;
                T pivot = arr[pivotPos];
                Swap(arr, low, pivotPos);

                int left = low;
                for (int i = low + 1; i <= high; i++)
                {
                    if (arr[i].CompareTo(pivot) < 0)
                    {
                        left++;
                        Swap(arr, i, left);
                    }
                }

                Swap(arr, low, left);
                return left;
            }

            #endregion

            #region Public Static Methods

            /// <summary> 
            /// Sequential quicksort. 
            /// </summary> 
            /// <typeparam name="T"></typeparam> 
            /// <param name="arr"></param> 
            public static void QuicksortSequential<T>(T[] arr, Func<T, T, int> comparer)
            {
                QuicksortSequentialInPlace(arr, 0, arr.Length - 1, comparer);
            }

            /// <summary> 
            /// Parallel quicksort 
            /// </summary> 
            /// <typeparam name="T"></typeparam> 
            /// <param name="arr"></param> 
            public static void QuicksortParallel<T>(T[] arr, Func<T, T, int> comparer)
            {
                QuicksortParallelInPlace(arr, 0, arr.Length - 1, comparer);
            }

            #endregion

            #region Private Static Methods

            public static void QuicksortSequentialInPlace<T>(T[] arr, int left, int right, Func<T, T, int> comparer)
            {
                if (right > left)
                {
                    int pivot = Partition(arr, left, right, comparer);
                    QuicksortSequentialInPlace(arr, left, pivot - 1, comparer);
                    QuicksortSequentialInPlace(arr, pivot + 1, right, comparer);
                }
            }

            public static void QuicksortParallelInPlace<T>(T[] arr, int left, int right, Func<T, T, int> comparer)
            {
                if (right > left)
                {
                    if (right - left < SEQUENTIAL_THRESHOLD)
                    {
                        QuicksortSequentialInPlace(arr, left, right, comparer);
                    }
                    else
                    {
                        int pivot = Partition(arr, left, right, comparer);
                        Parallel.Invoke(() => QuicksortParallelInPlace(arr, left, pivot - 1, comparer),
                                        () => QuicksortParallelInPlace(arr, pivot + 1, right, comparer));
                    }
                }
            }

            private static int Partition<T>(T[] arr, int low, int high, Func<T, T, int> comparer)
            {
                // Simple partitioning implementation 
                int pivotPos = (high + low) / 2;
                T pivot = arr[pivotPos];
                Swap(arr, low, pivotPos);

                int left = low;
                for (int i = low + 1; i <= high; i++)
                {
                    if (comparer(arr[i], pivot) < 0)
                    {
                        left++;
                        Swap(arr, i, left);
                    }
                }

                Swap(arr, low, left);
                return left;
            }
            #endregion
        }
    }
}

NPSF3000

Posted 2014-05-05T20:12:10.653

Reputation: 374

You should get around 200 results, more or less irrespective of the size of your input data. I suspect your problem is related to the way you're using binary search, in lines 98-102 - I suspect you're assuming that x.A will come from sortedA, and x.B will come from sortedB, whereas in fact both will come from sortedB, and this Comparer will produce nonsense results. – James_pic – 2014-05-12T14:32:24.063

More generally, if you're sorting by A and B, there's a faster algorithm than iterating on A and binary searching on B which is O(n log(n)) (and is effectively a poor-man's hash table). You can instead merge-join the two lists, which is O(n). – James_pic – 2014-05-12T14:35:40.783

Another fun option, since you know that the values of B will be uniformly distributed in a specific range, would be to swap binary search for interpolation search, which reduces search time from O(log(n)) to O(log(log(n)). – James_pic – 2014-05-12T14:38:15.347

@James_pic thanks for the suggests, I'll chase them up if I have time. I just cut 40 odd seconds out of my IO so I can focus again on the sort and computation. – NPSF3000 – 2014-05-12T14:47:19.467

Comparer fixed, results produced. Computation accounts for only about 5 seconds of my thirty (input 12, sorts 5 each) so I'm thinking about my next line of attack. IO is processing at ~100MBps so speedups might be limited. – NPSF3000 – 2014-05-12T23:25:01.383

Can you give me instructions for running it in linux? I tried mcs NPSF3000.cs which gives a couple of warnings and then mono NPSF3000.exe ~/largefile.file which gets as far as Started: Parallel Sort A but then does nothing it seems. – None – 2014-05-13T18:14:58.560

It looks like you are creating a huge number of threads. Do you mean to do that? – None – 2014-05-13T18:33:25.803

@Lembik, I don't run linux and have little time ATM to debug :( What are the warnings? Note: I don't create any threads, I simply queue work for the threadpool to handle for me. – NPSF3000 – 2014-05-13T22:32:22.790

The warnings are NPSF3000.cs(134,24): warning CS0219: The variable tasks' is assigned but its value is never used NPSF3000.cs(61,30): warning CS0219: The variablesolved' is assigned but its value is never used. It is a shame I can't run your code yet. The question does state that my system is ubuntu and I need to be able to run the code to test it. – None – 2014-05-14T08:43:05.733

@Lembik Ignore those warnings, they are fine. I understand it needs to run on your system (and it should, thanks to mono) I am just too busy to debug it myself (flying to Sydney in 10 hours, for a busy few days) – NPSF3000 – 2014-05-14T12:38:09.207

1

C

Brutal, brute force, in-your-face ugly C. On a do-over I would choose just about any other compiled language.

/*
Filter a file based on these rules:

Input:
    - each item is an ordered list of three integers ( A B T )
    - each line represents an item
    - each line is formated as <number> <w> <number> <w> <number>
    - <w> is whitespace (a single blank in the challenge)
    - <number> is an integer in the range 0..49_999_999
    - the first number on a line is A, second B, third T

Output a given item ( A B T ) if:
    1 - there exists an item ( C D U ) such that 0 <= T-U < 100 and D == A 
    OR
    2 - there exists an item ( C D U ) such that 0 <= U-T < 100 and B == C 

CLARIFICATION:
An item should be output only once, even if there is more than one match.

We're sorting on T, we know the number of Ts to be sorted and the Ts are random.
Trade space for speed and create a lookup table that can handle collisions
(AKA hash table).
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <pthread.h>
#include <assert.h>


#define NTHREADS    (16)
#define BINSPERTHREAD   (1*1000*1000)
bool    oneThread = false;

typedef struct {
    pthread_t   tid;
    long        begin;
    long        end;
} threadState;

void *initTID() {
    return NULL;
}


#define MAXITEMS    (50*1000*1000)
//  items on the boundary are not included in the search
#define SEARCHBOUNDARY  (100)


void usage(char *name) {
    fprintf(stderr, "usage: %s [-n 1..%d]\n", name, MAXITEMS);
}

typedef struct item {
    long    A;
    long    B;
    long    T;
    bool    unprinted;
    struct item *b;         // b(ackward to previous item)
    struct item *f;         // f(orward to next item)
    struct item *BINb;          // backward to previous bin
    struct item *BINf;          // forward to next bin
#ifdef DEVTEST
    long    lineNumber;
#endif
} item;
#ifdef DEVTEST
bool    printVerbose = false;
#endif


//  Why global variables? Because large MAXITEMS overflow the stack.
long    maxItems;           // entries allocated in list & lookup
item    *list;
long    listN;              // number of entries (max index + 1)
item    **lookup;
long    lookupN;            // number of entries (max index + 1)


/*
input -
    n       - index of current bin
    list        - global
    lookup      - global
    lookupN     - global
side-effects -
    list[] (.unprinted)
    stdout
*/
static inline void *walkThisBin(long n) {
    item    *p;
    item    *searchHead;
    item    *searchTail;
    item    *currentItem;
    long    i;

    //  for all items in bin
    for ( currentItem = lookup[n]; currentItem != lookup[n]->BINf;
        currentItem = currentItem->f)
    {
    /*
        merged forward&backward search
    */
    searchHead = currentItem;
    //  step to index min((T+100-1),lookupN-1), find largest U<T+100
    i = ((n+SEARCHBOUNDARY-1) < lookupN) ?
        n+SEARCHBOUNDARY-1 :
        lookupN-1;
    //  find largest i such that U-T<100 (U is lookup[i]->T)
    //  degenerate case is i == n
    for(p=lookup[i];
        !((p->T-searchHead->T)<SEARCHBOUNDARY);
        p=lookup[i]) {
        i--;
    }
    searchTail = p->BINf;       // boundary, not included in search
    p = currentItem;
    do {
        if (searchHead->B == p->A) {
        //  matches are symmetric
        if (searchHead->unprinted) {
            printf("%ld %ld %ld\n", searchHead->A, searchHead->B,
            searchHead->T);
            searchHead->unprinted = false;
        }
        if (p->unprinted) {
            printf("%ld %ld %ld\n", p->A, p->B, p->T);
            p->unprinted = false;
        }
        }
        p = p->f;
    } while (p!=searchTail);
    }
    return NULL;
}

/*
Must handle out-of-range indexes for lookup.

input -
    n       - index of current bin
    list        - global
    lookup      - global
    lookupN     - global
side-effects -
    list (.unprinted)
    stdout
*/

static inline void *walkTheseBins(void *tState) {
    long    startIndex = ((threadState *)tState)->begin;
    long    finishIndex = ((threadState *)tState)->end;
    long    n;

    startIndex = (startIndex<0) ? 0 : startIndex;
    finishIndex = (finishIndex>lookupN-1) ? lookupN-1 : finishIndex;
    for (n=startIndex; n<=finishIndex; n++) {
    walkThisBin(n);
    }
    return NULL;
}


int main(int argc, char *argv[]) {
#ifdef DEVTEST
item    *head;
item    *tail;
long    count = 0;
#endif
    //  subroutines? subroutines? we don't need no stinkin' subroutines
    //  this is all the scoping you're going to need
    //                      ... truuuuust me
    /*
    Allocate list[] and lookup[]. Set maxItems.

    input -
        argc
        argv
    side-effects -
        list
        lookup
        maxItems
        malloc()
        DEVTEST stuff
    */
    {
    int c;          // option character

    maxItems = MAXITEMS;
    while ((c = getopt(argc, argv, ":n:sv")) != -1) {
        switch(c) {
#ifdef DEVTEST
        case 'v':
        //  print some reassuring messages
        printVerbose = true;
        break;
#else
        case 'v':
        fprintf(stderr, "unknown option -%c\n", optopt);
        usage(argv[0]);
        exit(1);
        break;
#endif
        case 'n':
        if (sscanf(optarg, "%ld", &maxItems) != 1) {
            fprintf(stderr, "-n argument \"%s\" unscannable\n", optarg);
            usage(argv[0]);
            exit(1);
        }
        break;
        case 's':
        //  use only one thread?
        oneThread = true;
        break;
        case ':':           // -s needs an argument
        usage(argv[0]);
        exit(1);
        break;
        case '?':           // not a valid option
        fprintf(stderr, "unknown option -%c\n", optopt);
        usage(argv[0]);
        exit(1);
        break;
        }
    }
    if ((maxItems<1) || (maxItems>MAXITEMS)) {
        fprintf(stderr, "-s argument \"%ld\" out of range\n", maxItems);
        usage(argv[0]);
        exit(1);
    }
    list = (item *) malloc(sizeof(item) * maxItems);
    if (list == NULL) {
        fprintf(stderr, "ERROR: list = malloc() failure\n");
        exit(1);
    }
    lookup = (item **) malloc(sizeof(item *) * maxItems);
    if (lookup == NULL) {
        fprintf(stderr, "ERROR: lookup = malloc() failure\n");
        exit(1);
    }
    }

    /*
    Convert STDIN into an array of items.

    input -
        list
        lookup
        maxItems
    side-effects -
        list
        lookup
        listN
        stdin
    */
    {
    long    largestT = 0;
    item    x;

    for (listN=0; scanf("%ld%ld%ld", &x.A, &x.B, &x.T)==3; listN++) {
        if (listN == maxItems) {
        fprintf(stderr, "ERROR: > %ld input items read\n", maxItems);
        exit(1);
        }
        x.b = x.f = NULL;
        x.unprinted = true;
        x.BINb = x.BINf = NULL;
        largestT = (x.T>largestT) ? x.T : largestT;
#ifdef DEVTEST
        x.lineNumber = listN + 1;
#endif
        list[listN] = x;
    }
    if (!feof(stdin)) {
        fprintf(stderr, "ERROR: ferror() = %d\n", ferror(stdin));
        exit(1);
    }
    //  Be paranoid. Because cores are obnoxious.
    if (largestT>=maxItems) {
        fprintf(stderr, "ERROR: T:%ld > %ld \n", largestT, maxItems-1);
        exit(1);
    }
    }
#ifdef DEVTEST
(printVerbose) && printf("in: %ld\n", listN);
#endif
    //  Short-circuit on 0 items. Simplifies things like finding the head.
    if  (listN == 0) {
    exit(0);
    }

    /*
    Populate the lookup table. Build a doubly linked list through it.

    input -
        list
        lookup
        listN
    side-effects -
        list[]
        lookup[]
        lookupN
        DEVTEST stuff
    */
    {
    long    n;

    /*
        Populate the lookup table. The lookup table is an array-of-lists.
    The lists are LIFO. This is the most primitive of hashes, where the
    key, item.T, is used as the index into the lookup table.
    */
    for (n=0; n<maxItems; n++) {
        lookup[n] = NULL;
    }
    for (n=0; n<listN; n++) {
        long    t = list[n].T;

        if (lookup[t] == NULL) {
        lookup[t] = &(list[n]);
        } else {
        // collision
        list[n].f = lookup[t];  // forward pointer assigned
        lookup[t] = &(list[n]);
        }
    }
    /*
        Collapse lookup to squeeze out NULL references. This breaks
    the linear mapping between T value & lookup index, but worth it for
    simpler search logic. Build a doubly linked list of bins.
    */
    item    *previousBin = NULL;    // last non-NULL lookup entry
    lookupN = 0;
    for (n=0; n<maxItems; n++) {
        if (lookup[n] != NULL) {
        lookup[lookupN] = lookup[n];
        lookup[lookupN]->BINb = previousBin;
        if (previousBin) {
            previousBin->BINf = lookup[lookupN];
        }
        previousBin = lookup[lookupN];
        lookupN++;
        }
    }
    previousBin->BINf = NULL;

    /*
        Build a doubly linked list. The forward pointers already exist
    within each lookup table bin.
    */
    item    *p;
    item    *binHead;
    item    *previous;

    //  create a loop in each bin
    for (n=0; n<lookupN; n++) {
#ifdef DEVTEST
count++;
#endif
        binHead = lookup[n];
        for (p=binHead; p->f; p=p->f) {
        p->f->b = p;
#ifdef DEVTEST
count++;
#endif
        }
        p->f = binHead;
        binHead->b = p;
    }
    //  break the loops and connect them tail-to-head
#ifdef DEVTEST
head = lookup[0];
#endif
    previous = NULL;
    for (n=0; n<lookupN; n++) {
        binHead = lookup[n];
        p = binHead->b;     // p => tail of this bin list
        binHead->b = previous;  // connect bin head to list
        if (previous) {     // connect list to bin head
        previous->f = binHead;
        }
        previous = p;
    }
    previous->f = NULL;
#ifdef DEVTEST
tail = previous;
#endif
    }

#ifdef DEVTEST
if (printVerbose) {
    printf("out: %ld\n", count);

    //  run through the list forwards
    item    *p;
    count = 0;
    for (p=head; p; p=p->f) {
    count++;
    }
    printf("forwards: %ld\n", count);
    //  run through the list backwards
    count = 0;
    for (p=tail; p; p=p->b) {
    count++;
    }
    printf("backwards: %ld\n", count);
    /*
        //  print the list
        for (p=head; p; p=p->f) {
        printf("%ld %ld %ld\n", p->A, p->B, p->T);
        }
    */
}
#endif

    /*
    Find matches & print.

    (authoritative statement)
    Print item ( A B T ) if:
    1 - there exists an item ( C D U ) such that 0 <= T-U < 100 and D == A 
        OR
    2 - there exists an item ( C D U ) such that 0 <= U-T < 100 and B == C 


    TBD
    - threading


    input -
        lookupN
    side-effects -
        lots hidden in walkTheseBins(), all thread-local or thread-safe
    */
    {
    volatile threadState    tState[NTHREADS]; // use as cicular buffer
    long                h;  // cicular buffer head
    long                n;

    if (oneThread) {
        tState[0].begin = 0;
        tState[0].end = lookupN-1;
        walkTheseBins((void *)tState);
    } else {
        //  every slot has a thread to wait for
        for (h=0; h<NTHREADS; h++) {
        assert( pthread_create(&(tState[h].tid), NULL, initTID, NULL) == 0);
        }
        h = 0;
        for (n=0; n<lookupN+BINSPERTHREAD; n+=BINSPERTHREAD) {
        pthread_join(tState[h].tid, NULL);
        tState[h].begin = n;
        tState[h].end = n + BINSPERTHREAD - 1;
        assert( pthread_create(&(tState[h].tid), NULL, walkTheseBins, (void *)(tState+h)) == 0);
        h = (h + 1) % NTHREADS;
        }
        //  wait for any remaining threads
        for (h=0; h<NTHREADS; h++) {
        pthread_join(tState[h].tid, NULL); // may have already join'ed some
        }
    }
    }

    return 0;
}

Compile with "gcc -m64 -pthreads -O". Expects input on stdin. Runs multi-threaded by default. Use "-s" option to use only one thread.

Scott Leadley

Posted 2014-05-05T20:12:10.653

Reputation: 459

I get warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long int’ [-Wformat=] fprintf(stderr, "ERROR: T:%d > %d \n", largestT, listN-1); – None – 2014-05-12T16:22:56.533

@Lembik I edited the source for the compiler warning & will add to my makefile. I also added a sentence on how to use at end of post. I've got a threaded version coming, but I'd like to get a performance check of unthreaded performance on your machine. – Scott Leadley – 2014-05-12T16:48:10.777

This code is strangely slow for me (see timings in question). How does it compare to the Java submission or the other C submission for you? – None – 2014-05-12T18:45:06.477

I think your code doesn't allow T-U = 0. I wanted to test it on the file containing only the lines 18662170 45121353 3365641 (newline) 44329255 18662170 3365641 but it returns an error. – None – 2014-05-12T20:16:46.423

@Lembik Ahh, T must be < 50M, not the number of lines of input. I corrected that and added threading. – Scott Leadley – 2014-05-13T07:30:32.977

1

I finally got a chance to build a physical Ubuntu 14.04 system similar to Lembik's and do a post-mortem on my solution to this puzzle. In my choice of importance:

  1. The true master is James_pic, because he didn't optimize prematurely.
    • he had a plan
    • he executed the plan at a high level of abstraction (Scala) and refined it there
    • he refined it further in C
    • he didn't over-refine it (see next point)
  2. File system I/O time is probably the lower-bound on elapsed time for the target system.
    • Lembik alludes to this, i.e. "The winners ... both are almost as fast as wc!"
  3. Some of the reasons my original solution sucked was non-performant:
    • Locality of reference is the dominant factor on the target system.
    • Sorting on A or B is a good idea when doing a hash sort. Sorting on T adds complexity (and cache-hostile indirection) to the hash sort, at least the way I did it.
    • Scanf() is a pig.
    • Massive bandwidth (disk->memory->cache) changes where the bottlenecks are. The target system doesn't have massive bandwidth. (see next point)
  4. Rapid development works best if done in the target environment.
    • Duh! But, I was stuck with Solaris/SPARC originally and couldn't play otherwise.
    • It's difficult to eliminate caching effects in a virtualized and SAN environment.
    • Linux VMs typically have the same issues.
  5. A little bit of math helps.
    • Fetching one tuple directly from the hash table cuts the probability of indirect references to ~37% (~1/e).
    • Fetching two tuples directly from the hash table would have cut references to the overflow table to ~10%. It wasn't necessary.
  6. The 32-bit memory model (gcc -m32) is a distraction.
    • Sometimes a small win for unthreaded programs, sometimes a small loss.
    • Sometimes a significant loss for threaded programs.
    • If 32-bit is a significant win (and the target isn't an embedded controller), it's probably cheaper to refresh the hardware.
    • Take the extra registers and larger address space and don't look back.
  7. Scanf() is a pig, but using stdio is not hopeless.
    • Most of scanf()'s overhead appears to be in the format-driven parsing and string to integer conversion.
    • Replacing sscanf() with:
      • strtok() + atoi() is ~2x faster (see table below)
      • strtol() is ~3x faster
      • a custom, local strtol() is ~6.5x faster
      • replacing strtol() with a local solution puts it on par with "wc"
      • a FSM using getc_unlocked() is almost as fast as Keith Randall's minimalist mmap() solution
      • the results of my experiments while re-implementing in C [in CSV, since Stack Exchange apparently doesn't do tables]:
        
        "solution (64-bit unless noted)","disposition of input","user","system","elapsed"
        "dd if=? of=/dev/null bs=1024k","","0.010","1.107","26.47"
        "wc {LANG=C}","","4.921","0.752","26.38"
        "","","","",""
        "fscanf()","discard","13.130","0.490","26.43"
        "fgets(), no integer conversion","discard","1.636","0.468","26.42"
        "fgets() + sscanf()","discard","16.173","0.498","26.48"
        "fgets() + strtok(), no integer conversion","discard","4.659","0.481","26.48"
        "fgets() + strtok() + atoi()","discard","8.929","0.490","26.49"
        "fgets() + strtol()","discard","6.009","0.483","26.50"
        "fgets() + custom-strtol()","discard","3.842","0.474","26.43"
        "fgets() + custom-strtol()","sort (load hash) while reading","7.118","1.207","26.70"
        "fgets() + custom-strtol()","sort, match & print","10.096","1.357","28.40"
        "fgets() + custom-strtol(), 32-bit","sort, match & print","10.065","1.159","28.38"
        "","","","",""
        "james_pic's solution","sort, match & print","9.764","1.113","28.21"
        


Rather than boring you with yet another FSM parser, the solution below uses fgets() and a local strtol() replacement [look for s2i()].

A reference implementation in Ruby:

#!/usr/bin/ruby2.0
# only tested against ruby v1.9 & v2.0
=begin
Filter a file based on these rules:
Input:
  - each line is a set of three integers
  - each line is formatted as <number> <w> <number> <w> <number>
    - <w> is whitespace (a single blank in the challenge)
    - <number> is an integer in the range 1..50_000_000
Output a given tuple ( A B T ) if:
  - there exists a tuple ( C D U ) 0 <= T - U < 100 and D == A
    OR
  - there exists a tuple ( C D U ) 0 <= U - T < 100 and B == C

Typical use:
  filter.rb test.input | sort | uniq > test.output
=end
list = Array.new
lookupB = Hash.new { |hash, key| hash[key] = Array.new }
ARGF.each_with_index do |line, index|
  abt = line.split.map { |s| s.to_i }
  list << abt
  lookupB[abt[1]] << index
end
for abt in list do
  for i in Array( lookupB[abt[0]] ) do
    delta = abt[2] - list[i][2]     # T - U
    if (0<=delta) && (delta<100)
      puts "#{abt.join(' ')}"
      puts "#{list[i].join(' ')}"
    end
  end
end

It's a dog, ~50x slower than a C solution, but perl is just as slow and less concise.

The C solution:


#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
//      Throw caution, and error checking, to the winds.
// #include <assert.h>

#define RANGEMIN        (1)
#define RANGEMAX        (50*1000*1000)
#define SEARCHBOUNDARY  (100)
typedef struct {
    int             A;
    int             B;
    int             T;
} tuple_t;
typedef struct bin {
    tuple_t         slot;
    struct bin     *next;       // NULL=>0 items, self=>1 item, other=>overflow
} bin_t;
#define LISTSIZE        (RANGEMAX)
tuple_t         list[LISTSIZE];
#define HASH(x)         (x-1)
#define LOOKUPSIZE      (LISTSIZE)
bin_t           lookup[LOOKUPSIZE];
bin_t           overflow[LISTSIZE];
int             overflowNext = 0;

// based on strtol()
static inline int s2i(char *s, char **r)
{
    char            c;
    int             l = 0;

    do {
        c = *s++;
    } while (!isdigit(c));
    do {
        l = l * 10 + (c - '0');
        c = *s++;
    } while (isdigit(c));
    *r = s - 1;
    return l;
}

static inline void lookupInsert(tuple_t x)
{
    bin_t          *p = lookup + HASH(x.B);

    if (p->next) {
        overflow[overflowNext].slot = x;
        overflow[overflowNext].next = (p->next == p) ? p : p->next;
        p->next = overflow + overflowNext;
        overflowNext++;
    } else {
        p->slot = x;
        p->next = p;
    }
}

static void printOverflow(bin_t * head, bin_t * tail)
{
    if (head->next != tail) {
        printOverflow(head->next, tail);
    }
    printf("%d %d %d\n", head->slot.A, head->slot.B, head->slot.T);
}

static inline void dumpLookupSortedOnB()
{
    bin_t          *p;

    for (p = lookup; p < (lookup + LOOKUPSIZE); p++) {
        if (p->next) {
            printf("%d %d %d\n", p->slot.A, p->slot.B, p->slot.T);
            if (p != p->next) {
                printOverflow(p->next, p);
            }
        }
    }
}

static inline void printIfMatch(tuple_t abt, tuple_t cdu)
{
    int             A, B, T;
    int             C, D, U;

    A = abt.A;
    D = cdu.B;
    if (D == A) {
        T = abt.T;
        U = cdu.T;
        if ((0 <= (T - U)) && ((T - U) < SEARCHBOUNDARY)) {
            B = abt.B;
            C = cdu.A;
            printf("%d %d %d\n", A, B, T);
            printf("%d %d %d\n", C, D, U);
        }
    }
}

static inline void printMatches(int n)
{
    tuple_t        *p;

    for (p = list; p < (list + n); p++) {
        bin_t          *b = lookup + HASH(p->A);

        if (b->next) {
            bin_t          *q;

            printIfMatch(*p, b->slot);
            for (q = b->next; q != b; q = q->next) {
                printIfMatch(*p, q->slot);
            }
        }
    }
}

static inline void overflowTattle(int n)
{
    fprintf(stderr, "%d/%d items in overflow\n", overflowNext, n);
}

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

    // initialize lookup[]
    {
        bin_t          *p = lookup;

        for (n = 0; n < LOOKUPSIZE; n++) {
            p->next = NULL;
            p++;
        }
    }
    // read all tuples into list[] and insert into lookup[] & overflow[]
    {
        char            line[64];
        char           *lp;
        tuple_t        *p = list;

        for (n = 0; fgets(line, sizeof(line), stdin); n++) {
            p->A = s2i(line, &lp);
            p->B = s2i(lp, &lp);
            p->T = s2i(lp, &lp);
            lookupInsert(*p);
            p++;
        }
    }
    printMatches(n);
    exit(0);
}

Compile with "gcc -O3 -std=c99 -Wall -m64".

Scott Leadley

Posted 2014-05-05T20:12:10.653

Reputation: 459