problem: One terabyte This exercise asks you to work with a one-terabyte (240 =…
- User
problem: One terabyte This exercise asks you to work with a one-terabyte (240 = 1099511627776 bytes) stream of data consisting of 64-bit words. You are to compute the minimum value, the maximum value, and the median value of the data in the stream. The stream is supplied to you via an iterator that delivers 1048576 blocks, each of which consists of one megabyte (220 = 1048576 bytes, or 131072 64-bit words).
Perhaps needless to say, one terabyte is already a fair amount of data for a single machine to process, so be prepared to wait some time to finish the computations in this exercise.
Precise instructions may be found in the code package.
Hint. There is a practice stream with similar structure but less data that delivers a one-megabyte stream in 1024 blocks, each of which consists of one kilobyte (210 = 1024 bytes, or 128 64-bit words). Make sure your code works correctly on the practice stream before scaling up the problem.
An amusing contrast is perhaps that if we view the one-megabyte practice stream as having length one centimeter, then the one-terabyte stream is ten kilometers in length!
Complete the tasks in Scala 3. Only replace ??? symbols with your solution. Do not leave anything other out. Do not import external libraries.
Code: /*
- Description:
- This assignment asks you to study a stream of locks, each of
- which is an array of 64-bit words. The stream is implemented
- in the object inputStream, which has been instantiated
- from class BlockStream (see below). Access to the stream is by
- means of the Iterator interface in Scala, whose two methods
- hasNext() and next() tell you whether there are more blocks
- available in the stream, and if yes, return the next block in
- the stream, respectively. It is also possible to rewind()
- the stream back to its start.
- The stream in this assignment is a long one, exactly one terabyte
- (1099511627776 bytes) of data, divided into 1048576 blocks of 131072
- 64-bit words each. That is, 1048576 one-megabyte blocks.
- Remark:
- Observe that one terabyte of data is too much to store in main memory
- on most computers. Thus, whatever you do, keep in mind that it is perhaps
- not wise to try to store all the data in memory, or save the data
- to disk.
- Hints:
- Tasks 1 and 2 should not be extremely challenging. Think how to scan
- through the blocks. Task 3 is the most challenging one, most likely
- requiring multiple scans through the blocks, using the possibility
- to rewind(). Say, what if you looked at the most significant bits to
- Assistant
package teraStream: // ... (previous code) /* * Task 1: * Compute the minimum value in inputStream. * */ def minStream(s: BlockStream): Long = s.foldLeft(Long.MaxValue) { (minValue, block) => block.foldLeft(minValue) { (minValueInBlock, value) => math.min(minValueInBlock, value) } } end minStream val minValue = minStream(practiceStream) /* * Task 2: * Compute the maximum value in inputStream. * */ def maxStream(s: BlockStream): Long = s.foldLeft(Long.MinValue) { (maxValue, block) => block.foldLeft(maxValue) { (maxValueInBlock, value) => math.max(maxValueInBlock, value) } } end maxStream val maxValue = maxStream(practiceStream) /* * Task 3: * Assume that all the blocks in inputStream have been concatenated, * and the resulting array has been sorted to increasing order, * producing a sorted array of length 137438953472L. Compute the value at * position 68719476736L in the sorted array. (The minimum is at position 0L * in the sorted array, the maximum at position 137438953471L.) * * (See the remarks and hints above before attempting this task!) * */ def posStream(s: BlockStream, pos: Long): Long = { // Define function to count elements smaller or equal to "pivot" def countSmallerOrEqual(s: BlockStream, pivot: Long): Long = s.foldLeft(0L) { (count, block) => count + block.count(_ <= pivot) } var lowerBound = minValue var upperBound = maxValue while (upperBound - lowerBound > 1) { val mid = (lowerBound + upperBound) / 2 if (countSmallerOrEqual(s, mid) <= pos) { lowerBound = mid } else { upperBound = mid } } lowerBound } // end posStream val posValue = posStream(practiceStream, 68719476736L)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.