Skip to content

Add exercise parallel-letter-frequency - #1037

Merged
FridaTveit merged 1 commit into
exercism:masterfrom
matthewmorgan:add-exercise-parallel-letter-frequency
Dec 19, 2017
Merged

FridaTveit merged 1 commit into
exercism:masterfrom
matthewmorgan:add-exercise-parallel-letter-frequency

Conversation

@matthewmorgan

Copy link
Copy Markdown
Contributor

I will add this exercise to the track.


Reviewer Resources:

Track Policies

@matthewmorgan

Copy link
Copy Markdown
Contributor Author

Hmm. I think I may have painted myself into a corner. Turns out that implementing this is one thing, and creating a test suite that can verify that the user is employing some sort of concurrent processing is another.

Any recommendations on how to do that @exercism/java?

@stkent

stkent commented Nov 26, 2017 •

Copy link
Copy Markdown
Contributor

Hi, @matthewmorgan! Good to see you :)

My first thought would be: how are other tracks verifying the parallel nature of the implementation? Is there an approach we can steal borrow from one of them?

@matthewmorgan

matthewmorgan commented Nov 26, 2017 •

Copy link
Copy Markdown
Contributor Author

Hey, @stkent! Hope you are well.

Only a few tracks have implemented this, and in fact I saw the Python implementation just got pulled because non-parallel solutions were not being identified by the test suite.

The only approach I've seen that makes sense to me is on the Go track. They provide a single-threaded implementation of a solution and then benchmark the user's 'concurrent' solution against it. The other tracks (unless I missed something) seem to provide tests that even a sequential process would pass.

I think this exercise is worth doing, but I also feel like the nature of it is to demand a specific internal implementation. It's not enough to produce the right outputs from the given inputs, but we're also prying in to the internals of the solution, which feels weird, though perhaps necessary.

And, benchmarking doesn't prove or disprove concurrency. It might be able show that a solution is slower than could be expected, on a machine with X cores, when compared to a single-threaded solution run on the same machine, I guess?

I think Go's example may be the closest to useful, but I would be curious to hear your thoughts.

@sjwarner-bp

Copy link
Copy Markdown
Contributor

I would definitely agree with you @matthewmorgan, I think this exercise is absolutely worth implementing because of how different it is to the existing Java problems. Of course, with those valuable differences come some issues because we as a community aren't sure on the best ways of implementing them.

I'd be interested to see exactly what the figures are when benchmarking between the two different implementations (threaded and not threaded), and if the results are different enough then that could be a way to move forward.

Something I think could be relevant is our implementation of sieve. In the README.md, it explains that the implementation is not tested, and only the results are tested (i.e. the user is left with the responsibility that the sieve has been correctly implemented using the appropriate algorithm).

While we should obviously strive for the most complete testing strategy possible, if testing concurrency isn't strictly possible then I still see merit in this exercise, as prompts the user to use a certain implementation to solve a problem (and it is vastly different to existing exercises).

Of course, I'd be really interested in seeing what everyone else has to say, or if the Go stream's examples prove useful to us too! 🙂

@sjwarner-bp

Copy link
Copy Markdown
Contributor

I've just noticed something else! It seems as though the test suite for bank-account also has some idea of concurrency - have you checked this out @matthewmorgan ? 🙂

@matthewmorgan

Copy link
Copy Markdown
Contributor Author

@sjwarner-bp thanks for your input. I think the bank-account tests have a somewhat different goal in mind-- they're looking at allowing multiple threads to update the state of the account. Sort of, the inverse of what we want to do, I believe.

I'm still puzzling on this one...

@stkent

stkent commented Nov 27, 2017 •

Copy link
Copy Markdown
Contributor

Nothing brilliant springs to mind. Benchmarking seems fragile at best. Perhaps

While we should obviously strive for the most complete testing strategy possible, if testing concurrency isn't strictly possible then I still see merit in this exercise, as prompts the user to use a certain implementation to solve a problem (and it is vastly different to existing exercises).

is the right way to go here, with the real "checking" for this exercise happening (in theory) at the feedback stage*.

We could require solutions to accept an injected thread pool, and then benchmark the solution when the thread pool contains a single thread vs multiple threads? Forces quite a specific implementation, however.

*I am much more a maintainer than a mentor; I don't mean to pass the buck here.

@stkent

stkent commented Nov 27, 2017

Copy link
Copy Markdown
Contributor

@matthewmorgan

Copy link
Copy Markdown
Contributor Author

@stkent that's an interesting idea. That might be worth pursuing, thank you.

@matthewmorgan

Copy link
Copy Markdown
Contributor Author

@stkent @sjwarner-bp I'm planning on pursuing the strategy of injecting a thread pool. I think it's implementation-specific, as Stuart said, but I also think it may be the only way to capture the value of this exercise, and as Sam said, I think that promoting the concurrent approach has merit.

I'll submit something for review in the next few days.

@matthewmorgan
matthewmorgan force-pushed the add-exercise-parallel-letter-frequency branch 3 times, most recently from c6ab94e to f44c26b Compare December 2, 2017 20:38
@matthewmorgan

Copy link
Copy Markdown
Contributor Author

@exercism/java here you go.

There is no canonical test suite, so I used the tests on the C# track as the template. I added a final test that simply compares the performance of a 10-thread solution with a 1-thread solution in an attempt to find out if the user's solution can take advantage.

Let me know if there's anything you'd like changed or if you have any questions. Thanks!

@matthewmorgan
matthewmorgan force-pushed the add-exercise-parallel-letter-frequency branch from f44c26b to 1c45d26 Compare December 2, 2017 22:11

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great @matthewmorgan! :)

I've added some comments but they're pretty minor things, mostly about policies that have changed sligthly :)

import java.util.Map;


public class ParallelLetterFrequency {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and the public methods) could be package private :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!


public class ParallelLetterFrequency {

private static Map<Integer, Integer> map;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe calling it something like letterFrequencyMap would make it more clear what it is? :)

}

@Test
@Ignore

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be Ignore("Remove to run test") as explained in the POLICY doc :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ignore(...)s should also be placed above the @Test annotation, per the POLICIES doc!

Comment thread config.json Outdated
"slug": "parallel-letter-frequency",
"topics": [
"concurrency",
"type_conversion"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also maps? And maybe strings? And why type_conversion?

@matthewmorgan matthewmorgan Dec 4, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FridaTveit I'm happy to add those-- didn't know if I should just emphasize what made this different, or add more things and risk larding it up too much.

I put in type_conversion because of the way that int, char, and Integer are all used in the handling of the "letters". But if you think it's not appropriate I'm happy to remove it, LMK.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've been adding topics that users might encounter when implementing the exercise. Topics that make this exercise different are possibly most important but I think more common topics like strings and maps are important as well. Adding them means that users who particularly want to practice using maps can look for all exercises with that topic and then do them. That's how I'm imagining the topics could be used anyway, I might be wrong :)

I would only add type_conversion if a typical solution required explicitly casting from one type to another. And this exercise seems to only require you to count the frequency of each letter which you could do without any type conversions? But if you think type conversion would be part of a good solution then I'm happy for it to stay in :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right-- my solution uses that technique, but it wouldn't be required. It is used in the tests, but still, I don't think the user would need to use it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these topics still need updating? :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops...I did that, honest! I must have forgotten to commit the work. Thanks.

@matthewmorgan matthewmorgan changed the title Dibs: Add exercise parallel-letter-frequency Add exercise parallel-letter-frequency Dec 4, 2017
System.out.println("Single threaded computation took: " + singleJobTime + " ms.");
System.out.println("Multi threaded computation took: " + tenJobTime + " ms.");

assertTrue(singleJobTime - tenJobTime > 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A non-parallelized implementation might well pass this test with non-trivial frequency. Would it make sense to either

  1. run the computations many times for each pool, and average the results, or
  2. assert something like singleJobTime - 2 * tenJobTime > 0?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stkent I had originally done it like (2)! The results can vary quite a bit, but I think 2x is safe. It's usually been closer to 3x on my laptop.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet; let's go with 2 * as a first attempt then?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, um...on that: I did some testing this AM and was pretty consistently getting ~1.8x rather than two, which seems weird to me. Not sure what's changed since the first pass, but I'm going to investigate a bit more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤞

We could also drop to 1.5x?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stkent 1.5 seems to be working :). I also made a change where the number of threads defaults to the number of CPUs allocated to the JVM. Otherwise I think the performance of multiple threads might actually be worse in this case.

throws InterruptedException, ExecutionException {

ForkJoinPool singleThreadPool = new ForkJoinPool(1);
ForkJoinPool tenThreadPool = new ForkJoinPool(10);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super-familiar with ForkJoinPool. Does the mechanism we're using for testing place any implicit constraints on how solutions must be structured in order to leverage the multiple threads? If so, that might be something to call out in the hints file for this exercise.

@stkent

stkent commented Dec 5, 2017

Copy link
Copy Markdown
Contributor

Couple comments in, this is looking great!

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great :)

The build is failing because the last test is failing...

}

private Map<Integer, Integer> getMapFromLetters(String letters) {
Map<Integer, Integer> getMapFromLetters(String letters) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can still be private :)

@Ignore("Remove to run test")
@Test
@Ignore
public void tenThreadPoolIsFasterThanSingleThreadPool()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test failed when Travis ran it which failed the build :(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @FridaTveit I see, I wonder if that is an artifact of the CI environment..I am starting to feel like this test is too fragile. Let me work further on it.

@matthewmorgan
matthewmorgan force-pushed the add-exercise-parallel-letter-frequency branch from b894195 to efe1b57 Compare December 12, 2017 12:46
@matthewmorgan

Copy link
Copy Markdown
Contributor Author

@stkent @FridaTveit @sjwarner-bp

I've taken a different approach. This may be what some of you were suggesting at the beginning, but here's the idea:

  1. Make a pool of threads of a custom class, so we have some visibility into whether they've been started.
  2. Submit our solution to the pool.
  3. Ensure all threads in the pool were at least started. This should not happen with a single-threaded solution, and the test should fail.

I arbitrarily picked a 4-thread pool to ensure we had multiple threads. It would be possible for a user on a single core machine (Travis?) to get only a single thread if we peg the number of threads to the JVM cores. This would allow a single-thread solution to still pass, which we don't want.

This solution does away with benchmarking, which is clearly very brittle.

Let me know what you think, re @stkent's earlier comment about adding something to the docs about the required properties of the solution, I am happy to do that if this approach seems sound.

Thanks!

@stkent

stkent commented Dec 12, 2017

Copy link
Copy Markdown
Contributor

@matthewmorgan ingenious, I like it!

@matthewmorgan

Copy link
Copy Markdown
Contributor Author

@stkent I have to give credit to a friend at work @dbraley! The implementation was mine, but the concept follows suggestions from him, and of course all of you fine maintainers.

I'll noodle something for the docs.

@matthewmorgan

Copy link
Copy Markdown
Contributor Author

@exercism/java this is ready for a look

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! I have a couple of small comments but apart from those it looks ready to merge :)

Comment thread config.json Outdated
"slug": "parallel-letter-frequency",
"topics": [
"concurrency",
"type_conversion"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these topics still need updating? :)

Comment thread config.json Outdated
"concurrency",
"type_conversion"
],
"unlocked_by": null,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great it you could choose a core exercise for this to be unlocked by :) Normally we'd choose an easier core exercise with relatively similar topics. If you don't want to choose one that's also fine, we can open an issue for it :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chosenucleodtide-count. That's what I based my solution on to begin with. LMK if you think that's too big a leap.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be unlocked by a core exercise and nucleotide-count isn't a core exercise, sorry. nucleotide-count is unlocked by the core exercise hamming, so that might be a good one for this to be unlocked by as well?

@matthewmorgan matthewmorgan Dec 18, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sure @FridaTveit. Sorry about the misunderstanding! Hamming it is.

Create a function that returns the total frequency of each letter in a
list of texts and that employs parallelism.

# Hints

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be added in a hints.md file in the .meta directory as well so that if the readme is regenerated it will generate this bit too :) (see the contribution doc for details on how to generate the readme)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FridaTveit the docs are a little unclear to me.

  1. Do I simply add the hints.md at the .meta root? This seems to be what was done in EG pig-latin.
  2. I have changed the README.md to have a "Java Tips" section instead of a "Hints" section. Also, I have left the text in README.md, as I was unable to get the configlet tool to work after a couple of attempts.

I assumed the answer to both of the was 'yes'. If not, let me know and I may need some guidance on additional required changes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The answer to 1 is definitely yes :) For 2, I think that should be fine. I assume doing it manually is fine if you can't get configlet to work, @stkent?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's fine; the next time we run an all-track update it will get refreshed :)

Single-threaded (non-concurrent) solutions can pass all tests [but the last.](https://www.youtube.com/watch?v=mJZZNHekEQw) Your solution will be tested for concurrency by submitting it as a [Runnable](https://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html) to an [ExecutorService.](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html) Your solution must leverage multiple [Threads](https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html) to pass the final test.

Java documentation on [parallel streams](https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html) may provide some help.
# Running the tests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be an empty line before this heading?

@matthewmorgan
matthewmorgan force-pushed the add-exercise-parallel-letter-frequency branch from a9362c0 to 16f7ce9 Compare December 16, 2017 13:58
@matthewmorgan

Copy link
Copy Markdown
Contributor Author

OK @FridaTveit I've made the changes as requested. I didn't squash yet, happy to do it if you feel this is ready to go.

@matthewmorgan
matthewmorgan force-pushed the add-exercise-parallel-letter-frequency branch from 16f7ce9 to 54050fd Compare December 18, 2017 12:06

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks @matthewmorgan :)

@FridaTveit
FridaTveit merged commit 98e8464 into exercism:master Dec 19, 2017
@matthewmorgan

Copy link
Copy Markdown
Contributor Author

@FridaTveit Thanks for your help!

I hope people have fun with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants