Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:56:14+00:00 2026-05-27T14:56:14+00:00

I am writing a piece of software for parallel computations. The software is totally

  • 0

I am writing a piece of software for parallel computations. The software is totally generic, and thus I need a class to wrap sequences.

Right now, I am experiencing strange behaviour in my map-function. A map function takes a function-object as input, which can output a different type than its input argument.

Take a look at the following code:

class SeqMPJ[T](indexedSeq: IndexedSeq[T]) {

  val seq = indexedSeq.view //Lazy
  val freeRanks = MPJEnv.getFreeRanks(seq.size)
  //Get n free ranks
  val commGroup = MPI.COMM_WORLD.group.Incl(freeRanks.toArray)
  //Communicator for this sequence
  val comm = MPI.COMM_WORLD.Create(commGroup)

  val opIndex = globalRank % seq.size
  var operand: Any = seq(opIndex)

  if (!isOnline)
    error("Cannot use MPJ-abstractions outside parallelize body...")

  //Only works for p=n now
  def mapMPJ[U](f: (T) => U): SeqMPJView[U] = {
    if (freeRanks.contains(globalRank)) { //Process is part of this operation
      operand = f(operand.asInstanceOf[T])
    }
    return new SeqMPJView(operand.asInstanceOf[U], comm, freeRanks)
  }

…

Notice that in the function mapMPJ[U](f: (T) => U):SeqMPJView[U] , the function f has input type T and output type U. This means, after applying f to the variable “operand”, operand is of type U, however, this happens inside the if-block. In other words, depending on the state, operand has either type U or T. Now, when I cast to U, it always succeeds. Even when the condition in the if-block fails. As I see it, the program should fail when casting operand.asInstanceOf[U] if the program does not enter the if-block.

An example usage is in this matrix multiplication:

val M = 2
val N = 2

val A = Array(
  Array(1.0, 2.0),
  Array(3.0, 4.0))

val B = Array(
  Array(1.0, 2.0),
  Array(3.0, 4.0))

val Bt = B.transpose

/*
 * DNS using underlying MPI
 */
parallelize(args) {

  for (i <- 0 until M; j <- 0 until N)
    A(i) zip Bt(j) mapMPJ { case (a, b) => a * b } reduceMPJ (_ + _)

  if (globalRank == 0)
    println("CHECK RESULT:\n" + Matrix(A) * Matrix(B))

}

The program compiles and runs perfectly using the newest eclipse scala IDE. I haven’t tried other compilers, but it’s probably me being blind, but I spent so much time, so I hope for some help 🙂

edit

There’s an implicit conversion from Array to seqMPJ, FYI.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:56:15+00:00Added an answer on May 27, 2026 at 2:56 pm

    Casting is just you asserting to the compiler that you know what you are doing and what the type really is, when it comes to generic type arguments. They’re all really just AnyRef==java.lang.Object (or whatever the bounding type is). If you lie to it, it will believe you (until there is a runtime exception caused by having the wrong type somewhere that the type is used). If you want to know whether you’ve got the correct type, you have to check with manifests.

    Here’s an example:

    def example[A: ClassManifest,B: ClassManifest](a: A) = {
      if (implicitly[ClassManifest[A]] <:< implicitly[ClassManifest[B]]) a.asInstanceOf[B]
      else throw new Exception("Wrong!")
    }
    

    If you try a cast that won’t work, this will yell at you:

    scala> example[List[Int],List[String]](List())
    java.lang.Exception: Wrong!
    

    You can modify your code accordingly, e.g.

    implicitly[ClassManifest[U]].erasure.isAssignableFrom(operand.getClass)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a piece of simulation software, and need an efficient way to test
I need to use libcurl in a piece of software I am writing on
I'm writing a piece of software using RealBASIC 2011r3 and need a reliable, cross-platform
I am writing a piece of small software to go through the folders and
I'm writing my first proper useful piece of software. Part of it will involve
I'm writing a plug-in for a piece of software that takes a big collection
I am writing a piece of software in C++ RAD studio 2010 and got
I am writing a piece of software in C# using .NET 2 which detects
If I was writing a piece of software that attempted to predict what word
I'm writing a piece of honeypot software that will have extensive logging of interactions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.